|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
arraylist copyI 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 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 Show quoteHide quote "Sam" <samuel.berthe***@voila.fr> wrote in message news:1112977491.821616.100760@o13g2000cwo.googlegroups.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 > "Joseph MCAD" <anonym***@microsoft.discussions.com> schrieb: That is not true. 'Clone' will create a shallow copy of the arraylist:> If you use the .Clone method > then both arraylists will still point to the same objects. HTH \\\ 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/> 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 Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:OlmslNGPFHA.580@TK2MSFTNGP15.phx.gbl... > "Joseph MCAD" <anonym***@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/> Joseph,
"Joseph MCAD" <anonym***@microsoft.discussions.com> schrieb: I didn't find any occurance of "deep copy" in the 'ArrayList.CopyTo' > Thanks! Then what is the difference between CopyTo and Clone? MSDN > mentioned that clone creates a shallow copy and copyto creates a deep > copy? 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/> "Joseph
> Thanks! Then what is the difference between CopyTo and Clone? MSDN A clone makes a copy of the arraylist. While an array is a reference type, > mentioned that clone creates a shallow copy and copyto creates a deep > copy? Thanks again! > 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 Thanks Cor, that does help.
John On Sat, 9 Apr 2005 09:21:15 +0200, "Cor Ligthert" <notmyfirstn***@planet.nl> wrote: Show quoteHide quote > >"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 > April 9, 2005
Thanks for everybody's replies! Joseph MCAD Show quoteHide quote "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:ucqaDTNPFHA.3076@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 > Try the .Clone() method of the ArrayList.
Marcie Show quoteHide quote On 8 Apr 2005 09:24:51 -0700, "Sam" <samuel.berthe***@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 April 8, 2005
I am a little confused. Do I have the functions of the Clone and CopyTo methods mixed up? Thanks! Joseph MCAD Show quoteHide quote "Marcie Jones" <marciejo***@yahoo.com> wrote in message news:h0dd51pn6pkhkflf9ipqvlikmtetnqk21v@4ax.com... > Try the .Clone() method of the ArrayList. > > Marcie > > On 8 Apr 2005 09:24:51 -0700, "Sam" <samuel.berthe***@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 >
Show quote
Hide quote
"Sam" <samuel.berthe***@voila.fr> schrieb: Both variables point to the same instance of 'ArrayList'. You can use the > 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 ? 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/> What does "shallow" and "deep" copy mean?
TIA, John On Fri, 8 Apr 2005 19:50:50 +0200, "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote: Show quoteHide quote >"Sam" <samuel.berthe***@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)) >/// "J L" <j***@marymonte.com> schrieb: An insight into cloning objects in .NET> What does "shallow" and "deep" copy mean? <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/> 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? Show quoteHide quote "Herfried K. Wagner [MVP]" wrote: > "J L" <j***@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/> >
How to detect when items are added to Combobox/Listbox
TextChanged Excel Reports in VB.NET Inheriting forms problem How do you force a thread to run on a specific processor? bin folder Opening and Closing a form error : Cannot access a disposed object named "frmImage". Validate User Change/assign a value to dataset How to remove a window-Icon |
|||||||||||||||||||||||