Create a "clone" of this object, do not point to it

Say I have a list called

myFirstList 

And then I want to create a copy of this list so that I can make some custom settings. So I do this:

 mySecondList = myFirstList mySecondList.doTweaks 

But I noticed that the settings also affect the myFirstList object! I just want tweaks to influence the second ...

And after that I want to completely remove mySecondList , so I do mySecondList = Nothing , and I'm right, right?

+8
visual-studio
source share
7 answers

But I noticed that tricks also affect the myFirstList object! only I want the changes to affect the second one ...

Of course yes. Both variables point to the same object in memory. Everything you do with one happens to the other.

You will need to make a deep clone or shallow, depending on your requirements. This article should give you a better idea of ​​what you need to do.

+6
source share

Since you have not divulged the type of item that you save in your list, I assume that it is something that implements IClonable (otherwise, if possible, implement IClonable or determine how to clone a single item in the list)

Try something like this

 mySeconmySecondList = myFirstList.[Select](Function(i) i.Clone()).ToList() 
+5
source share

Adam Rakis, I do not like your "Of course it is," because it is not at all obvious.

If you have a string variable that you assign to another string variable, you do not change them when you make changes to one of them. They do not point to the same physical part of the memory, so why is it obvious what classes do?

In addition, the case is not even agreed. In the following case, you will have all the elements in the array pointing to the same object (they all end with the Number variable set to 10:

 SourceObject = New SomeClass For i = 1 To 10 SourceObject.Number = i ObjectArray.Add = SourceObject Next i 

BUT, the following will give you 10 different instances:

 For i = 1 To 10 SourceObject = New SomeClass SourceObject.Number = i ObjectArray.Add = SourceObject Next i 

Apparently, the scope of the object matters, so it’s not at all obvious what is happening.

+4
source share

Here's how you do it:

 'copy one object to another via reflection properties For Each p As System.Reflection.PropertyInfo In originalobject.GetType().GetProperties() If p.CanRead Then clone.GetType().GetProperty(p.Name).SetValue(clone, p.GetValue(OriginalObject, Nothing)) End If Next 
+3
source share

Extension on Adam Rackies ' answer I was able to implement the following code using VB.NET.

My goal was to copy a list of objects that served mainly as data transfer objects (i.e. database data). The first dtoNamedClass class is defined and the ShallowCopy method is added. A new variable is created with the name dtoNamedClassCloneVar, and the LINQ select query is used to copy the variable of the dtoNamedClassVar object.

I was able to make changes to dtoNamedClassCloneVar without affecting dtoNamedClassVar .

 Public Class dtoNamedClass ... Custom dto Property Definitions Public Function ShallowCopy() As dtoNamedClass Return DirectCast(Me.MemberwiseClone(), dtoNamedClass) End Function End Class Dim dtoNamedClassVar As List(Of dtoNamedClass) = {get your database data} Dim dtoNamedClassCloneVar = (From d In Me.dtoNamedClass Where {add clause if necessary} Select d.ShallowCopy()).ToList 
+2
source share

this works for me:

 mySecondList = myFirstList.ToList 
+1
source share

a clone is an object to which you are cloning.

 dim clone as new YourObjectType 

You say so.

0
source share

All Articles