1

How can I get an item from a list as a new copy/instance, so I can use and change it later without changing the original object in the list?

Public Class entry
    Public val As String
     ' ... other fields ...
End Class

Dim MyList As New List(Of entry)
Dim newitem As New entry
newitem.val = "first"
MyList.Add(newitem)

Now if I try to get an item from this list and change it to something else, it changes the original item in the list as well (it is used as a reference not as a new instance).

Dim newitem2 As New entry
newitem2 = MyList.Item(0)
newitem2.val = "something else"

So now the MyList.item(0).val contains "something else", yet I wanted only the newitem2 to contain that new value for the given field and retain other values from the object in the list.

Is there a way to do this without reassigning all fields one by one?

Emi
  • 296
  • 1
  • 5
  • 18

1 Answers1

2

If entry is defined as a reference type (Class), then your only option is to explicitly create a new instance that has the same values as the originals. For example:

Public Partial Class Entry
    Public Function Clone() As Entry
        Return New Entry() With { .val = Me.val, … }
    End Function
End Class

(The .NET Framework Class Library defined a type ICloneable from early on for exactly this purpose. The type never really caught on for certain reasons.)

Be aware that you might have to do this recursively, that is, if your class contains fields that are of a reference type, you'll have to clone the objects stored in these fields as well.

Then, instead of doing this:

Dim newitem2 As New entry  ' (Btw.: `New` is superfluous here, since you are
newitem2 = MyList.Item(0)  '         going to throw away the created instance here.)

Do this:

Dim newitem2 As Entry = MyList.Item(0).Clone()

One alternative is to use value types (i.e. declare your item type as Structure). Value types are automatically copied when passed around. However, there are lots of caveats to observe, among them:

  • Do not do this if your type contains many fields. (Why? Many fields usually means that the type will occupy more bytes in memory, which makes frequent copying quite expensive if the objects get too large.)

  • Value types should be immutable types. (Why? See e.g. Why are mutable structs evil?)

These are just two guidelines. You can find more infornation about this topic here:

Community
  • 1
  • 1
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Thank you for the extensive answer. A lot of useful information. Now I'm thinking about going with something like `IClonable`, but I'll read up on `Struct` as well. – Emi Apr 02 '15 at 07:54