473,387 Members | 1,440 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,387 software developers and data experts.

Parameter contents changing after class creation

I'm having trouble with a class that accepts an ArrayList as a
parameter. The code looks something like this:

ArrayList arrayOne = new ArrayList();

arrayOne.Add("Hello ");
arrayOne.Add("There!");

someClass sc1 = new someClass(arrayOne);

arrayOne.Clear();
arrayOne.Add("Oh ");
arrayOne.Add("No!");

someClass sc2 = new someClass(arrayOne);

If I step through the code, sc1 is created just fine with the
appropriate contents of arrayOne. However, one I reach the Clear()
call, the contents of the ArrayList inside of sc1 change and "Oh No!"
is then placed inside. In the end I have sc1 and sc2 with identical
ArrayLists. I'm sure there's something obvious I'm missing, any help
would be greatly appreciated.
Nov 25 '07 #1
2 1315
Hello Pi****@gmail.com,

Just to undestand why this happens your need to understand that arrayList
intance is created in heap, and you send the reference on the single instance
of your arraylist to both classes. So any action to arrayList in each of
these classes will change your arrayList.

To avoid this you need create another new instance of arrayOne and use it
in sc2 class

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

I'm having trouble with a class that accepts an ArrayList as a
parameter. The code looks something like this:

ArrayList arrayOne = new ArrayList();

arrayOne.Add("Hello ");
arrayOne.Add("There!");
someClass sc1 = new someClass(arrayOne);

arrayOne.Clear();
arrayOne.Add("Oh ");
arrayOne.Add("No!");
someClass sc2 = new someClass(arrayOne);

If I step through the code, sc1 is created just fine with the
appropriate contents of arrayOne. However, one I reach the Clear()
call, the contents of the ArrayList inside of sc1 change and "Oh No!"
is then placed inside. In the end I have sc1 and sc2 with identical
ArrayLists. I'm sure there's something obvious I'm missing, any help
would be greatly appreciated.

Nov 25 '07 #2
Just to undestand why this happens your need to understand that arrayList
intance is created in heap, and you send the reference on the single instance
of your arraylist to both classes. So any action to arrayList in each of
these classes will change your arrayList.

Thanks, Michael! Got it figured out now. Thanks again.
Nov 25 '07 #3

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

Similar topics

5
by: Michelle Kinsey-Clinton | last post by:
Hello, I am writing an ASP app which is giving me some very frustrating errors. They appear intermittently, no real pattern to them, and often go away if you reload, or back up a few pages and...
8
by: Bob Smith | last post by:
I am downloading over http port 80 some contents froma site, but the contents is not properly stored after using ostringstream for temporary storage, and later ostringstream::str() for passing it...
4
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. ...
6
by: Al Wilkerson | last post by:
Hi, Does anyone know how to display the contents of a DataGrid on another aspx page. I have a aspx page that when the user clicks on a link button the database is queried and I want the results...
1
by: William H. Burling | last post by:
i have a nest of vb.vanilla collections. CollectionofHotelData.item("great Pequot").item("2000").item(22) The above works. I wrote the code that constructs the nested collections and i can...
20
by: Brien King | last post by:
If I have a parameter that has an Object type (as opposed to something like a string), can I make that parameter a CONST? Right now, if you pass an object into a sub/function, that sub/function...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
4
by: Kuldeep | last post by:
Hi All, I am trying to read the contents of a page through its URL. My code snippet is as follows: public void mtdGetPageDataHWR() { HttpWebRequest objRequ =...
3
by: alan | last post by:
Hello all, I'm wondering if it's valid for a templated class to use a template parameter as a base class. Basically I have two abstract types, one of which is derived from the other, and I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.