By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

arraylist copy

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
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

P: n/a
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

P: n/a
Sam
thanks, that's clone indeed :)

Nov 21 '05 #4

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
JL

See my message to Joseph

Cor
Nov 21 '05 #13

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.