2 Replies Latest reply on Jun 4, 2009 4:30 AM by *Prashant Shelke*

    writing a efficient copy routine

    stevoad Level 1

      Hi,

       

      I have to update a ValueObject I have created, with a list of parameters that are passed as another object. So my value object has name, age, address etc, my second object has objID, createdDate, parameters, in this parameters property is a list of the values I want to use to update my value object. So the parameters contain name, age, address etc. The slight problem is this list of parameters can change for each object so one may have name, age, address while the next may have just age, address. It depends on what has changed.

       

      I was thinking of passing both objects to a copy function, then in this function loop through the parameters list property and then check if each parameter property is in the parameters list and then update the value object property. This would lead me a function that contains a lot of if statements like this:

       

      public function copyParameters( objToCopy:Object, objToCopyTo:ValueObj ):void

      {

           if( objToCopy.parameters["name"] != null )

           {

                objToCopyTo.name = objToCopy.parameters["name"];

           }

           if( objToCopy.parameters["age"] != null )

           {

                objToCopyTo.age = objToCopy.parameters["age"];

           }

           if( objToCopy.parameters["address"] != null )

           {

                objToCopyTo.address = objToCopy.parameters["address"];

           }

           etc....

      }     

       

       

      This could lead to a long list of if statements, is there a more efficient way of writing this kind of function?

      Thanks

       

      Stephen

        • 1. Re: writing a efficient copy routine
          dzeikei Level 2

          have you tried:

           

          var objectCopy:Object = ObjectUtil.copy(object) ?

          ObjectUtil.copy() uses built in Flash Player AMF capabilities so it's better/faster if you want an exact copy

          But maybe this is not what u r looking for....

           

          you could do:

           

          public function copyObj(object:Object, vo:VOObjec):void

          {

              for (var p:String in object)

               {

                    vo.parameters[p] = object[p];

               }

          }

          1 person found this helpful
          • 2. Re: writing a efficient copy routine
            *Prashant Shelke* Level 4

            This can do the same..


            public static function clone (objet:Object):Object
                    {
                        var buffer:ByteArray = new ByteArray();
                        buffer.writeObject(objet);
                        buffer.position = 0;
                        var result:Object = buffer.readObject();
                        return result;
                    }


            Thanks.

            1 person found this helpful