8 Replies Latest reply on Jun 16, 2010 4:45 AM by Ibarim

    arrayCollection modification changes other arrayCollections

    Ibarim Level 1

      Hi,

      I need to create an arrayCollection as a copy of other arrayCollection. Then I need to change some data inside the copy, but when I do that the original arrayCollection changes as well. Is there a way to stop the changes in the original arrayCollection?

      For example I got: arrColOrig[0].someVar = 1

       

      I've tried:

       

      arrColCopy = new arrayCollection (arrColOrig.source)

      arrColCopy[0].someVar = 2

       

      effect is that arrColOrig[0].someVar = 2

       

      I've tried also:

       

      arr = new Array

      arr = arrColOrig.source

      arrColCopy = new arrayCollecion (arr);

      arrColCopy[0].someVar = 2

       

      effect is that 'someVar' changes to value '2' in arr and in arrColCopy and in arrColOrig

       

      So is there a way to maintain the original arrayCollection unchanged? I know that I can create the copy by looping the original  arrayCollection, but I'm looking for a simpler way.

        • 1. Re: arrayCollection modification changes other arrayCollections
          David_F57 Level 5

          hi,

           

          You always have to be careful when copying objects as most of the time all you are really doing is creating a pointer which can become a disaster, Objectutil is the safest way to make copies.

           

          import mx.utils.ObjectUtil;

          var myCollection: ArrayCollection = ObjectUtil.copy(originalArrayCollection);
          David

          1 person found this helpful
          • 2. Re: arrayCollection modification changes other arrayCollections
            Ibarim Level 1

            Thanks for help,

            I've tried your solution but there is one problem, I get error:

            Implicit coercion of a value with static type Object to a possibly unrelated type mx.collections:ArrayCollection

             

            Have you met this error when using ObjectUtil?

            • 3. Re: arrayCollection modification changes other arrayCollections
              David_F57 Level 5

              hi,

               

               

              thats what happens when you do things of the top of your head,  i forgot the important bit.....

               

              arr2 = ObjectUtil.copy(arr1) as ArrayCollection;

               

              David

              • 4. Re: arrayCollection modification changes other arrayCollections
                Ibarim Level 1

                Thanks, works like a charm:)

                 

                As my arrayCollection is filled with object I was getting to use loop:

                for (i=0;i<arr1.length;i++) {
                       arr2.addItem(ObjectUtil.copy(arr1[i]));
                }

                 

                and it worked, but coping whole array at once is much more simple.

                • 5. Re: arrayCollection modification changes other arrayCollections
                  BhaskerChari Level 4

                  Hi lbarim,

                   

                  We can also use the below code to copy the array collection:

                   

                  var copyArrayCol : ArrayCollection = new ArrayCollection(orgArrayCol.toArray());

                   

                   

                  Thanks,

                  Bhasker Chari

                  • 6. Re: arrayCollection modification changes other arrayCollections
                    Ibarim Level 1

                    Hi BhaskerChari,

                     

                    I was trying your solution, but the effect is the same: original array changes when you modify it's copy.

                    I was trying also various combinations of arrCol.source and arrCol.source.slice(), but none of it worked. It was strange, becouse 'slice()' method should create a copy (it's said in adobe livedocs).

                    So I'll stick to David_F57 solution with 'ObjectUtil.copy()'.

                    • 7. Re: arrayCollection modification changes other arrayCollections
                      BhaskerChari Level 4

                      Hi lbarim,

                       

                      Check the below application and also observe my comments:

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
                      <mx:Script>
                        <![CDATA[
                        import mx.utils.ObjectUtil;
                        import mx.collections.ArrayCollection;
                       
                        private var orgArrayCol:ArrayCollection =  new ArrayCollection([{label:"ALL",data:null},
                                  {label:"OTHER",data:"OTHER"},
                                  {label:"SOME",data:"PARTIAL"}]);

                        private function copyArrayCollection():void
                        {
                         var copyArrayCol : ArrayCollection = new ArrayCollection(orgArrayCol.toArray());
                        
                         // No chnage on original ArrayCollection performing these two operations
                         copyArrayCol.removeItemAt(0);
                         copyArrayCol.addItemAt(orgArrayCol.getItemAt(0),0);
                        
                         // You should not use this approach for changing a property at particular index
                         // You need to use below appraoch
                         // If you use this approach it will change the label property in both the ArrayCollection
                         copyArrayCol.getItemAt(0).label = "SOME ALL";

                         // You need to follow this appraoch to change any property(say label) at any particular index
                         // This will chnage the label property only in copyArrayCol ArrayCollection
                         var obj:Object = copyArrayCol.getItemAt(0);
                         obj.label = "SOME ALL";
                         copyArrayCol.setItemAt(obj,0);
                        }
                       

                        ]]>
                      </mx:Script>
                      </mx:Application>

                       

                       

                      Thanks,

                      Bhasker Chari

                       

                      1 person found this helpful
                      • 8. Re: arrayCollection modification changes other arrayCollections
                        Ibarim Level 1

                        In my case I needed to change the property at particular index - that's why

                        code:

                        'copyArrayCol[0].label = "new label" '

                        didn't work for me when I used:

                        'var copyArrayCol : ArrayCollection = new

                        ArrayCollection(orgArrayCol.toArray());'.

                         

                        I don't have time to check your other solution, when I got one working

                        already (ObjectUtil.copy), so I have to trust you that it works as well

                         

                        Thanks for your time