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: -
ArrayList alTemp1 = new ArrayList();
-
ArrayList alTemp2 = (ArrayList)Session["AStandingSessionAL"];
-
-
for (int i = 0; i < alTemp2.Count; i++) {
-
alTemp1.Add((clsAnObject)alTemp2[i]);
-
((clsAnObject)alTemp1[i]).Price = 46.00;
-
}
-
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...
10 1422
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: -
ArrayList alTemp1 = new ArrayList();
-
ArrayList alTemp2 = (ArrayList)Session["AStandingSessionAL"];
-
-
for (int i = 0; i < alTemp2.Count; i++) {
-
alTemp1.Add((clsAnObject)alTemp2[i]);
-
((clsAnObject)alTemp1[i]).Price = 46.00;
-
}
-
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: -
alTemp1.Add((clsAnObject)alTemp2[i]);
-
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
Yea, frinny is right. It's not so much that the ArrayList is being funnny as it is normal behaviour for objects.
Consider: -
Form myform1 = new Form();
-
Form myform2 = myform1;
-
//this is roughly what you are doing in your array list copying
-
Thanks, but how do I "clone the object" while add ing it?
Have you tried?
alTemp1.Add( ((clsAnObject)alTemp2[i]).Clone() );
Doesn't work because my "custom" class/object does not have a method "clone"...
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.
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
Thanks, I built a "Copy" constructor
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#
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#
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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: 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,...
| |