Preserving Properties in New instance of class

Skip Navigation LinksIrocon > Blog > 2003 > November, 2003 > November 11, 2003 > Preserving Properties in New instance of class

Preserving Properties in New instance of class

Great post by Edneeis on Gotdotnet VB.NET forum
------------------------------------------------------------------
The question was: how do I initialize an instance of a class and have all the values and properties from the old class in the new class

A: You can implement ICloneable and give your object its own MemberwiseClone implemention which is generally protected in scope. You could also use serialization to clone the object, providing it is declared serializable. So instead of initializing a new instance with New you would use a shared function that returns a variant of the copied one.

Public Shared Function GetClone(original as foo) As foo
'serialize foo to a memory stream
Dim result as foo
'deserialize the memory stream to the result variable and return it
Return result
End Function

'syntac to make a copied foo
Dim newfoo As foo=foo.GetClone(oldfoo)
'now you'd have a seperate instance of foo which has all the same property values as the old

Last Updated:Thursday, September 02, 2010By:alfero#

Would you like to comment?

Sign in with your OpenID. If you do not know what OpenID is, find out.