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

c# ArrayList Question

I can't seem to "disconnect" a "rebuilt" ArrayList of objects from it's source AL; I thought I understood the rules on copying AL Data objects vs. the "clone" Method (and it's simply being a reference to the original ArrayList). The code looks like:
Expand|Select|Wrap|Line Numbers
  1.            ArrayList alTemp1 = new ArrayList();
  2.            ArrayList alTemp2 = (ArrayList)Session["AStandingSessionAL"];
  3.  
  4.             for (int i = 0; i < alTemp2.Count; i++) {
  5.                 alTemp1.Add((clsAnObject)alTemp2[i]);
  6.                 ((clsAnObject)alTemp1[i]).Price = 46.00;
  7.             }
  8.  
This code will change the "Price" to 46.00 for all objects in the ORIGINAL ArrayList (Session["AStandingSessionAL"]). I thought that if you created a new AL and did "Add" from another AL, this would "disconnect" or "Deep Copy" the AL??

Thanks...
Mar 12 '08 #1
10 1422
Frinavale
9,735 Expert Mod 8TB
I can't seem to "disconnect" a "rebuilt" ArrayList of objects from it's source AL; I thought I understood the rules on copying AL Data objects vs. the "clone" Method (and it's simply being a reference to the original ArrayList). The code looks like:
Expand|Select|Wrap|Line Numbers
  1.            ArrayList alTemp1 = new ArrayList();
  2.            ArrayList alTemp2 = (ArrayList)Session["AStandingSessionAL"];
  3.  
  4.             for (int i = 0; i < alTemp2.Count; i++) {
  5.                 alTemp1.Add((clsAnObject)alTemp2[i]);
  6.                 ((clsAnObject)alTemp1[i]).Price = 46.00;
  7.             }
  8.  
This code will change the "Price" to 46.00 for all objects in the ORIGINAL ArrayList (Session["AStandingSessionAL"]). I thought that if you created a new AL and did "Add" from another AL, this would "disconnect" or "Deep Copy" the AL??

Thanks...
When you do:
Expand|Select|Wrap|Line Numbers
  1.    alTemp1.Add((clsAnObject)alTemp2[i]);
  2.  
You are are adding the object's memory address (at i) to alTemp1...which is the same memory address as alTemp2. Therefore when you change the Price of the object, both ArrayLists are updated (since they're pointing to the same object).

You should clone the object while adding it to alTemp1.

-Frinny
Mar 12 '08 #2
Plater
7,872 Expert 4TB
Yea, frinny is right. It's not so much that the ArrayList is being funnny as it is normal behaviour for objects.

Consider:
Expand|Select|Wrap|Line Numbers
  1. Form myform1 = new Form();
  2. Form myform2 = myform1;
  3. //this is roughly what you are doing in your array list copying
  4.  
Mar 12 '08 #3
Thanks, but how do I "clone the object" while add ing it?
Mar 12 '08 #4
Plater
7,872 Expert 4TB
Have you tried?
alTemp1.Add( ((clsAnObject)alTemp2[i]).Clone() );
Mar 12 '08 #5
Doesn't work because my "custom" class/object does not have a method "clone"...
Mar 12 '08 #6
Plater
7,872 Expert 4TB
Doesn't work because my "custom" class/object does not have a method "clone"...
Well then you willd need to figure out a method for copy/cloning your custom object.
Mar 12 '08 #7
Frinavale
9,735 Expert Mod 8TB
Well then you willd need to figure out a method for copy/cloning your custom object.
I know this is a slight bit off topic but, can you over load operators in C#?
You cannot in VB.NET (found out yesterday and wasn't to impressed).

-Frinny
Mar 12 '08 #8
Thanks, I built a "Copy" constructor
Mar 12 '08 #9
Plater
7,872 Expert 4TB
I know this is a slight bit off topic but, can you over load operators in C#?
You cannot in VB.NET (found out yesterday and wasn't to impressed).

-Frinny
Yeah I believe you can define what is done on a + or - or various other things like that in C#
Mar 12 '08 #10
Frinavale
9,735 Expert Mod 8TB
Yeah I believe you can define what is done on a + or - or various other things like that in C#
It's pretty handy to be able to tell the compiler what to do with your object when + or - (...==..??) operators are used on your object.

Using VB has completely removed the benefits that come from this....

>>Gur<<

I wish I worked in C#
Mar 12 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
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...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
11
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
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:
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
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:
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.