3 Replies Latest reply on Jul 17, 2008 4:34 AM by Culme

    Readonly ArrayCollection

    Culme
      Hi!
      I have a class which, among other things, contains an ArrayCollection.

      I would like that arraycollection to be readable from other classes, but it should only be "writable" from within the class itself.

      Since AS is by reference, simply declaring the arraycollection private and provide only a getter method won't help - the reference will obviously still allow stuff like "removeItemAt(n)" and so on.

      I was thinking about declaring it private, and then have a getter method that returns a COPY of the arraycollection - but it wouldn't be quite perfect. You could still change items in the copy, and thus be fooled into believing that you had updated the original arraycollection.

      Does anyone have any hints on this?

      Thanks in advance!

      /Culme
        • 1. Re: Readonly ArrayCollection
          atta707 Level 2
          is it possible for you to return a reference of mx.collection.IList instead of ArrayCollection?

          If so, you can simply extend ArrayCollection class and throw on all the methods of IList interface that modify the collection in any way. And when it comes to returning a reference of type IList, just return a reference of this new arraycollection (which would essentially be a copy of original arrayclloection only this one would throw on any modification attempts)

          makes sense?

          ATTA
          • 2. Re: Readonly ArrayCollection
            Culme Level 1
            IList... hmm... yes that should do it. :-)
            I'll give it a try!

            Thanks!

            Best regards
            Culme
            • 3. Re: Readonly ArrayCollection
              Culme Level 1
              I set out to extend ArrayCollection and implement IList, and it will certainly work.
              I'm not even sure I'll bother implementing IList; perhaps I'll just try to figure out which methods would be "dangerous" in this particular case, and just override them.

              The only drawback is that the errors obviously won't be thrown until the app is actually running. It would have been nice to hide the undesired methods altogether - so they wouldn't even show up in the code hinting. (Is that doable..?)

              Anyway, here's a tiny snippet of what I'll be doing in the first place.

              Thanks again!

              /Culme