ArrayList consisting of objects

G

Guest

Hi,
Here is a piece of code where I have a class by name SomeClass
array1 consists of objects of the type SomeClass
array2 is intialized with contents of array1.
Why does changes made to the object of array2 affect the same object in
array1.

The o/p for the following should be just '1' but why is it '11'?

ArrayList array1 = new ArrayList();
array1.Add(new SomeClass("1"));
array1.Add(new SomeClass("2"));

ArrayList array2 = new ArrayList();
array2 = array1;
array1.Add(new SomeClass("3"));


SomeClass al = (SomeClass) array2[0];
al.AlName = "11";

SomeClass newObj = (SomeClass) array1[0];
MessageBox.Show (newObj.AlName);
 
S

Steven Nagy

Hi

This is standard behaviour for "Reference" types.
You should have a read about the difference between value types and
reference types (just google it). Class instances are reference types.

SN
 
C

Cor Ligthert [MVP]

In addition to Steven,

Have a look at the keyword "new"
"new" tells that it is a new array (you are instancing that from a Class
running its instancing method)

Without it it is telling that it is referencing (setting the reference) to
an existing one.

Cor
 
J

Jon Skeet [C# MVP]

ZS said:
Here is a piece of code where I have a class by name SomeClass
array1 consists of objects of the type SomeClass
array2 is intialized with contents of array1.
Why does changes made to the object of array2 affect the same object in
array1.

The o/p for the following should be just '1' but why is it '11'?

ArrayList array1 = new ArrayList();
array1.Add(new SomeClass("1"));
array1.Add(new SomeClass("2"));

ArrayList array2 = new ArrayList();

Calling new ArrayList() here is pointless when you're about to reassign
the value:
array2 = array1;

This makes the variables array2 and array1 both have values which are
*references* to the same ArrayList. Think of it as two people each with
a piece of paper which has the address of the same house. If one person
goes to the house and puts a chair in it (adds an item to the list, in
this case), then the other person will see that chair when they go to
the house.

<snip>

See http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html for more information on
reference types and value types.
 
C

Cor Ligthert [MVP]

ZS,

That is completely depending for what you want to do, there are not for
nothing both solutions.

Cor

ZS said:
So how can I change this piece of code so that it does not occur.

-Zelma

ZS said:
Hi,
Here is a piece of code where I have a class by name SomeClass
array1 consists of objects of the type SomeClass
array2 is intialized with contents of array1.
Why does changes made to the object of array2 affect the same object in
array1.

The o/p for the following should be just '1' but why is it '11'?

ArrayList array1 = new ArrayList();
array1.Add(new SomeClass("1"));
array1.Add(new SomeClass("2"));

ArrayList array2 = new ArrayList();
array2 = array1;
array1.Add(new SomeClass("3"));


SomeClass al = (SomeClass) array2[0];
al.AlName = "11";

SomeClass newObj = (SomeClass) array1[0];
MessageBox.Show (newObj.AlName);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top