473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ArrayList consisting of objects

ZS
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);

Sep 14 '06 #1
5 1439
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

Sep 14 '06 #2
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

"ZS" <ZS@discussions.microsoft.comschreef in bericht
news:03**********************************@microsof t.com...
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);
Sep 14 '06 #3
ZS <ZS@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 14 '06 #4
ZS
So how can I change this piece of code so that it does not occur.

-Zelma

"ZS" wrote:
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);
Sep 14 '06 #5
ZS,

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

Cor

"ZS" <ZS@discussions.microsoft.comschreef in bericht
news:58**********************************@microsof t.com...
So how can I change this piece of code so that it does not occur.

-Zelma

"ZS" wrote:
>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);
Sep 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jens Stjärna | last post by:
Hi. I have a question regarding the ArrayList. In my code I use the arraylist to store objects of certain class. I do not mix object types in the same ArrayList. public ArrayList adresses = new...
9
by: TT ( Tom Tempelaere ) | last post by:
Hi At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
0
by: Just D. | last post by:
There is an interesting article - http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542&printer=t It shows how to serialize the ArrayList of identical objects. I did it a year...
0
by: Johann Zöhrer | last post by:
Hello, I didn't find a resource in the Internet, where I can find how to move rows of a DataGrid consisting of an ArrayList. The Microsoft example,...
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.