Home All Groups Group Topic Archive Search About
Author
14 Dec 2005 6:55 PM
Roy
Are there any collection classes that will take object references to that if
I add an object (could be primitive data types) to it and I change it through
the original object, the one in the collection classes will be changed
automatically too?
The ArrayList would make a copy of the original and therefore would not
reflect the change.

Author
14 Dec 2005 7:06 PM
Vadym Stetsyak
ArrayList would make a copy of value type.
If reference type will be added to arraylist then only reference to the
object is stored, that is:

SomeObj someObj;
arrayList.Add(someObj);

//change the object
someObj.SomeProp = newValue;

//then
SomeObj newObj = arraList[index];

newObj and someObj will be equal thus their inner state will be the same.
newObj.SomeProp equals to newValue

For value types you have to get access to the value with the help of
container methods.
e.g.

int i = 5;
arrayList.Add(5);
i = 6;

int newOne = (int)arraList[index];
newOne will be 5;

that is why you have to do the action through a wrapper e.g.
arrayList.SetAt(index, i)

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Roy" <R**@discussions.microsoft.com> wrote in message
news:9BBF27E4-85F5-4D53-9146-2443AF9B732F@microsoft.com...
> Are there any collection classes that will take object references to that
> if
> I add an object (could be primitive data types) to it and I change it
> through
> the original object, the one in the collection classes will be changed
> automatically too?
> The ArrayList would make a copy of the original and therefore would not
> reflect the change.

AddThis Social Bookmark Button