472,353 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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 8215
April 8, 2005

Switch the line rNodeListNew = rNodeList to:

rNodeList.CopyTo(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.googlegr oups.com...
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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*********@yahoo.com> wrote in message
news:h0********************************@4ax.com...
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.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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 #5
"Sam" <sa**************@voila.fr> schrieb:
Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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*******@microsoft.discussions.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*************@TK2MSFTNGP15.phx.gbl...
"Joseph MCAD" <an*******@microsoft.discussions.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*******@microsoft.discussions.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.CopyTo'
documentation. 'CopyTo' IMO doesn't create a deep copy too. However, there
is a difference between 'ArrayList.Clone' and 'ArrayList.CopyTo': '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.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(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))
///


Nov 21 '05 #10
"J L" <jo**@marymonte.com> schrieb:
What does "shallow" and "deep" copy mean?


An insight into cloning objects in .NET
<URL:http://www.codeproject.com/dotnet/Clone.asp>

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

"Joseph
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!


A clone makes a copy of the arraylist. While an array is a reference type,
are only the references copied. Any change in a shadow will affect on the
other one as well. Except when it is by instance the removing or adding of a
reference (row), than they grew apart.

A copyto makes a copy of the values in a one dimensional array. Where when
it are objects it is in fact again a shadow of course.

A deep copy makes new copy of all the values and creates a new arraylist
arround those. For that I have never seen a method in Net. You have to do it
yourself. (In my opinion can that be complicated when it is by instance a
jagged array).

I hope this gives some idea's

Cor
Nov 21 '05 #12
JL

See my message to Joseph

Cor
Nov 21 '05 #13
If I am reading the articles correctly then if an object implements the
Icloneable interfact and it is cloned, then for all the value types the value
is copied to the new object and for all the reference types, only the
reference(pointers) are copied. So what is gained? If I have an arraylist
of value type objects then the .Clone Method will copy the values to a new
arraylist but if the arraylist objects are reference types then only the
reference is copied to the new arraylist. Then when I change a value type in
the new arraylist, the original arraylist is not changed but If I change a
reference type in the new arrraylist, then the original arraylist will also
reflect that change since they both point to the same instance of the object.
Is all this correct?

"Herfried K. Wagner [MVP]" wrote:
"J L" <jo**@marymonte.com> schrieb:
What does "shallow" and "deep" copy mean?


An insight into cloning objects in .NET
<URL:http://www.codeproject.com/dotnet/Clone.asp>

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

Nov 21 '05 #14
J L
Thanks Cor, that does help.

John

On Sat, 9 Apr 2005 09:21:15 +0200, "Cor Ligthert"
<no************@planet.nl> wrote:

"Joseph
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!


A clone makes a copy of the arraylist. While an array is a reference type,
are only the references copied. Any change in a shadow will affect on the
other one as well. Except when it is by instance the removing or adding of a
reference (row), than they grew apart.

A copyto makes a copy of the values in a one dimensional array. Where when
it are objects it is in fact again a shadow of course.

A deep copy makes new copy of all the values and creates a new arraylist
arround those. For that I have never seen a method in Net. You have to do it
yourself. (In my opinion can that be complicated when it is by instance a
jagged array).

I hope this gives some idea's

Cor


Nov 21 '05 #15
April 9, 2005

Thanks for everybody's replies!

Joseph MCAD
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...

"Joseph
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!


A clone makes a copy of the arraylist. While an array is a reference type,
are only the references copied. Any change in a shadow will affect on the
other one as well. Except when it is by instance the removing or adding of
a reference (row), than they grew apart.

A copyto makes a copy of the values in a one dimensional array. Where when
it are objects it is in fact again a shadow of course.

A deep copy makes new copy of all the values and creates a new arraylist
arround those. For that I have never seen a method in Net. You have to do
it yourself. (In my opinion can that be complicated when it is by instance
a jagged array).

I hope this gives some idea's

Cor

Nov 21 '05 #16

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

Similar topics

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...
1
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...
7
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...
3
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...
7
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...
6
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...
8
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...
5
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...
22
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();...
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. ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.