Copy object with .Net?

  • Thread starter Thread starter Grahammer
  • Start date Start date
G

Grahammer

In VB6 I had to create "Clone" method if I wanted to generate an independant
copy of an object.

In VB.Net, is it possible copy an object so that you can change the copy
without altering the original, or do I still need to use a self created
Clone method?

Thanks!
 
Grahammer,

To create a shallow copy of your object (i.e. just value fields, but
references to other objects will be the same), use the MemberwiseClone
(built into objects) function. Example:
myNewObject = DirectCast(myObject.MemberwiseClone, myType)

However, if you want deep copy your object, you will need to have your class
implement the IClonable interface.

-- Alex Papadimoulis
 
Back
Top