473,856 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arraylist copy

Sam
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remov eAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx

Nov 21 '05 #1
15 8346
April 8, 2005

Switch the line rNodeListNew = rNodeList to:

rNodeList.CopyT o(rNodeListNew)

This should COPY the objects into the new arraylist. This way the
arraylists do not point to the same objects. If you use the .Clone method
then both arraylists will still point to the same objects. HTH

Joseph MCAD

"Sam" <sa************ **@voila.fr> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remov eAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx

Nov 21 '05 #2
Try the .Clone() method of the ArrayList.

Marcie

On 8 Apr 2005 09:24:51 -0700, "Sam" <sa************ **@voila.fr> wrote:
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remo veAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew . Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx


Nov 21 '05 #3
Sam
thanks, that's clone indeed :)

Nov 21 '05 #4
April 8, 2005

I am a little confused. Do I have the functions of the Clone and CopyTo
methods mixed up? Thanks!

Joseph MCAD
"Marcie Jones" <ma*********@ya hoo.com> wrote in message
news:h0******** *************** *********@4ax.c om...
Try the .Clone() method of the ArrayList.

Marcie

On 8 Apr 2005 09:24:51 -0700, "Sam" <sa************ **@voila.fr> wrote:
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNe w = rNodeList
rNodeList.Rem oveAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNe w. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx

Nov 21 '05 #5
"Sam" <sa************ **@voila.fr> schrieb:
Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remov eAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?


Both variables point to the same instance of 'ArrayList'. You can use the
arraylist's 'Clone' method to create a shallow copy of the arraylist:

\\\
Dim al1 As New ArrayList
al1.Add("Bla")
al1.Add("Goo")
Dim al2 As ArrayList = al1.Clone()
al1.Clear()
MsgBox(CStr(al2 .Count))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
"Joseph MCAD" <an*******@micr osoft.discussio ns.com> schrieb:
If you use the .Clone method
then both arraylists will still point to the same objects. HTH


That is not true. 'Clone' will create a shallow copy of the arraylist:

\\\
Dim a1 As New ArrayList
Dim a2 As ArrayList = a1.Clone()
MsgBox(CStr(a1 Is a2))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #7
April 8, 2005

Thanks! Then what is the difference between CopyTo and Clone? MSDN
mentioned that clone creates a shallow copy and copyto creates a deep copy?
Thanks again!

Joseph MCAD
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:Ol******** *****@TK2MSFTNG P15.phx.gbl...
"Joseph MCAD" <an*******@micr osoft.discussio ns.com> schrieb:
If you use the .Clone method then both arraylists will still point to the
same objects. HTH


That is not true. 'Clone' will create a shallow copy of the arraylist:

\\\
Dim a1 As New ArrayList
Dim a2 As ArrayList = a1.Clone()
MsgBox(CStr(a1 Is a2))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
Joseph,

"Joseph MCAD" <an*******@micr osoft.discussio ns.com> schrieb:
Thanks! Then what is the difference between CopyTo and Clone? MSDN
mentioned that clone creates a shallow copy and copyto creates a deep
copy?


I didn't find any occurance of "deep copy" in the 'ArrayList.Copy To'
documentation. 'CopyTo' IMO doesn't create a deep copy too. However, there
is a difference between 'ArrayList.Clon e' and 'ArrayList.Copy To': 'CopyTo'
can be used to copy data to an array. Overloaded versions of 'CopyTo' are
provided which allow you to specify what data should be copied.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
J L
What does "shallow" and "deep" copy mean?

TIA,
John

On Fri, 8 Apr 2005 19:50:50 +0200, "Herfried K. Wagner [MVP]"
<hi************ ***@gmx.at> wrote:
"Sam" <sa************ **@voila.fr> schrieb:
Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remov eAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?


Both variables point to the same instance of 'ArrayList'. You can use the
arraylist's 'Clone' method to create a shallow copy of the arraylist:

\\\
Dim al1 As New ArrayList
al1.Add("Bla ")
al1.Add("Goo ")
Dim al2 As ArrayList = al1.Clone()
al1.Clear()
MsgBox(CStr(al 2.Count))
///


Nov 21 '05 #10

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

Similar topics

3
1898
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 pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy the pItem is also cleared. I guess I need a "deep copy" but can't find any documentation on it. ...
1
3402
by: Marc Lefebvre | last post by:
there is an easy way to make a deep copy of an arraylist ? suppose I have an arraylist of COptions implements ICloneable I have need to overload the Clone() methode of a derived ArrayList (OptionsArrayList) And Overload the Clone() methode of my COptions There is an otherway to do deep copy ?
7
14297
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list to move up the elements, then put the tmp object in last pos is the best way? Cheers --...
3
9700
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
7
2094
by: Adam | last post by:
Hi all, In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and how I might modify a structure in an arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change items based on certain conditions. Thanks in advance!
6
4337
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new or saved Arraylist, how is it done. It always seems to create a shallow copy i.e. by Reference. I even tried passing the original Arraylist to a function by Value and return a copy....after an arraylist.clear on the original it still clears the...
8
3142
by: Esmail Bonakdarian | last post by:
Hello, I am relatively new to C# but like it. I have run into a problem to which I haven't been able to find a solution despite looking. I have an ArrayList that contains several RichTextBoxes (via Add()) I am trying to create a second ArrayList with this information, but I want to make a DEEP copy instead of a shallow one, so that
5
2477
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I want to create a copy of the arrayList, which I can do looping through the
22
15968
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b = (ArrayList) a.Clone();
3
2636
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: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
0
9908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9760
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11052
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9530
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7930
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3198
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.