Lucas, what you are doing with line A = B is like saying "Make variable A
reference to the same object as variable B".

Kinda like:

//Declare two variables that will reference any instance of a class that
inherits from Object, directly or not.
var var1 : Object;
var var2 : Object;

//Create two instances of custom classes and reference each of them with the
two different variables, var1 and var2

var1 = new ClassA; //let's call this new object 'objectA'
var2 = new ClassB; //let's call this one 'objectB'

Up to here, references are as follow:

var1 -> an instance of ClassA (we know it as 'objectA')
var2 -> an instance of ClassB (we know it as 'objectB')

But then, you do this:

var1 = var2;

which is stating: "Make var1 reference the object presently referenced by
var2"

So our object world lies as follows:

var1 -> an instance of ClassB (this is the object that was referenced by
var2 when the last line executed; we called this one 'objectB')
var2 -> an instance of ClassB (this didn't change since last time, it stills
references 'objectB').

Now, since both variables are referencing the SAME object, when accessing
any of both variables you'll be accessing the same object.

In this scenario, 'objectA' would cease to exist in the next passing of the
Garbage Collector, since it is being referenced by no other object.

What you wanted to do in the first place was to 'clone' or 'copy' an object,
and referencing it by a variable. I believe there is a clone() or copy()
method in the Flex ObjectUtils, but I don't know if it resolves in a shallow
copy or a deep copy of the receiver. Anyhow, I hope this was helpful, good
luck!

Guido.

On 4/30/07, Lucas Pereira < [EMAIL PROTECTED]> wrote:

  Hi all.
Today I have this beginner question.

If I create Object A of ArrayCollection for instance.

And the create Object B also Array Collection, and Make B = A (it
safes a lot of time and code, specially when the other object is a
custom one).

If I add an item to B, that item is also added to A.

Other example. I have the class Guy with properties 1stName and 2ndName.

If A is instance of Guy and B is also instance of Guy and I want it to
have the same 1stName has A is do B = A.

Problem... If I make B.2ndName = "somename"... A object 2ndName also
changes to "somename".

I don't want this!!! I only want B = A at the beginning and then I
want to be free to change be without changing A.

How to get around this?

Thanks

And sorry for this silly question!!!




--
No puedo con todo...

Reply via email to