4 Replies Latest reply on Jun 22, 2010 10:25 PM by BhaskerChari

    Creating a deep copy of a complex object.

    alexblahblahblah

      Hi I am trying to create a copy of an complex object. When I say complex I mean an object which contains arraycollections which contain other objects, which can also contain other arrays/objects etc...


      I know that when I use ObjectUtil.clone(), it creates a copy of itself, but forgets the type as well as the types of the containing objects. I have been looking up registerClassAlias() to maintain the type that is being copied. However I am not sure how this is done. Does this mean I have use registerClassAlias() on all objects and attributes that is contained as well?

       

      If so I was wondering if someone could show me an easy example to follow.For instance if we have a Object A, it contains an Array Collection that stores Object B.


      How would you clone Object A? and keep the types B inside the ArrayCollection?

       

       

       

      Thanks,

      Alex

        • 1. Re: Creating a deep copy of a complex object.
          Wrentech

          Hi Alex,

          I agree, this is a difficult task. ObjectUtil.clone() works but it does not retain the class definitions of any of the properties/classes within the custom object being copied. Unfortunately I don't believe you can typecast these plain objects back to their original types either, unless you enforce strict data typing.

           

          Good luck,

          Warren

          • 2. Re: Creating a deep copy of a complex object.
            BhaskerChari Level 4

            Hi Alex,

             

            You do can clone an array collection and still keeping the data types same...check the below line of code..

             

             

            cloneArrayCollection = ObjectUtil.copy(originalArrayCollection) as ArrayCollection;

             

            The above line of code will clone the originalArrayCollection and copy it to the cloneArrayCollection while retaining the datatypes.

             

            If this post answers your question or helps, please kindly mark it as such.


            Thanks,

            Bhasker Chari

            1 person found this helpful
            • 3. Re: Creating a deep copy of a complex object.
              alexblahblahblah Level 1

              Hi, I tried to do as you suggested:

               

              var m:BudgetGridModel = ObjectUtil.copy(model) as BudgetGridModel;

               

              However what I get back from the copy is null.

               

              Any ideas?

               

               

              Thanks,

              Alex

              • 4. Re: Creating a deep copy of a complex object.
                BhaskerChari Level 4

                Hi Alex,

                 

                Is the data type of your model is BudgetGridModel or an ArrayCollection..???

                 

                What is the structure of your model variable...

                 

                Is it the following way...?

                 

                model = ArrayCollection..

                             [0]BudgetGridModel

                             [1]BudgetGridModel

                             [2]BudgetGridModel

                 

                If your model is an ArrayCollection of BudgetGridModel objects...you can simply use the same way I posted earliar...

                 

                var m:ArrayCollection = ObjectUtil.copy(model) as ArrayCollection;

                now m will be your new copy of model which is an arraycollection  of BudgetGridModel objects..

                 

                Thanks,

                Bhasker Chari