4 Replies Latest reply on May 29, 2007 7:58 AM by Nerun

    Assigning values vs. assigning object references

    Nerun
      I'm feeling kinda stupid to ask this, but is there a way not to "reference" an object when assigning it to a variable? when i have "a = x" and then "b = x" i want "a" and "b" to just hold the value, not the link to the source object.

      it sounds like a case of rtfm, but i just can't find doc answering that question.

      thanks in advance,
      Nerun

        • 1. Re: Assigning values vs. assigning object references
          dazweeja
          I'm a bit confused by your terminology. I think maybe you want to copy or clone an object?

          http://livedocs.adobe.com/flex/201/langref/mx/utils/ObjectUtil.html#copy()

          "This method is designed for copying data objects, such as elements of a collection. It is not intended for copying a UIComponent object, such as a TextInput control. If you want to create copies of specific UIComponent objects, you can create a subclass of the component and implement a clone() method, or other method to perform the copy."

          Cheers,
          Darren.
          • 2. Assigning values vs. assigning object references
            Nerun Level 1
            thanks for the reply dazweeja. i intentionally did not use words like "clone" or "copy", because i wanted to keep it more general. i read about the function you quoted and tbh it was what kind of caused my question.

            Yes i basically want to copy and object and i wanted to confirm my understanding that if you assign an object (e.g. myHBox1 = myOldHBox) you always assign not only the value but a reference to the object. If you do something like "myHBox2 = myOldHBox" afterwards, it will cause an error when "myHBox1" is used again, because it will basically be empty.

            Now talking of the quote you posted. If i have a custom class that is e.g. a HBox with some VBoxes in it and maybe a Grid and a Chart. How do i copy this? Even if I recursively go down into sub objects, every time I assign a value again i will run into the same problem, won't I?

            It's all really confusing for me, because I can't put my finger on the point where assigning a value will not cause an error.

            Thanks,
            Nerun
            • 3. Re: Assigning values vs. assigning object references
              dazweeja Level 1
              Yes, I see - not as easy as it sounds. I haven't tried this but maybe you could try writing the object into a ByteArray and then returning the new object?

              import flash.utils.ByteArray;

              function clone(source:Object):*
              {
              var myBA:ByteArray = new ByteArray();
              myBA.writeObject(source);
              myBA.position = 0;
              return(myBA.readObject());
              }

              Cheers,
              Darren.
              • 4. Re: Assigning values vs. assigning object references
                Nerun Level 1
                thanks dazweeja, i appreciate your help. in fact that last post sounds kinda interresting.

                i wonder why something like that isn't implemented by Adobe.