Copy of class instance

C

Chris Pratt

I'm sorry to ask such a fundamental question, but is it possible to create a
copy of an object in VB.Net 2005?

I have an app which, on load, creates an instance of a class, into which it
reads a load of data from XML. What I want is to be able to declare a new
instance of this class and make it a copy of the existing instance. This
way I can spawn various instances with their own updated properties

The code "Dim NewInst as MyClass = OldInst" creates a reference to the
existing instance and, therefore, changing any properties of NewInst are
reflected in OldInst.

Is there a simple way to do what I need?

Many thanks for any help.
 
C

Chris

Chris said:
I'm sorry to ask such a fundamental question, but is it possible to create a
copy of an object in VB.Net 2005?

I have an app which, on load, creates an instance of a class, into which it
reads a load of data from XML. What I want is to be able to declare a new
instance of this class and make it a copy of the existing instance. This
way I can spawn various instances with their own updated properties

The code "Dim NewInst as MyClass = OldInst" creates a reference to the
existing instance and, therefore, changing any properties of NewInst are
reflected in OldInst.

Is there a simple way to do what I need?

Many thanks for any help.

I don't know of a way to automatically do this, but you really need to
make a clone method and then inside the clone method set all the
properties to what they are in the current instance.

You may be able to do this with reflection in just a couple lines of
code. This sample code will do all the public properties.

Dim NewInstance As New IPEGCompany
Dim myType As Type = GetType(IPEGCompany)
Dim myProperties() As Reflection.PropertyInfo =
myType.GetProperties((Reflection.BindingFlags.Public Or
Reflection.BindingFlags.Instance))
' Loop thru the public properties
Dim PropertyItem As Reflection.PropertyInfo
For Each PropertyItem In myProperties
PropertyItem.SetValue(NewInstance,
Reader.GetValue(Reader.GetOrdinal(PropertyItem.Name)), Nothing)
Next
 
J

Jim Wooley

I'm sorry to ask such a fundamental question, but is it possible to
create a copy of an object in VB.Net 2005?

I have an app which, on load, creates an instance of a class, into
which it reads a load of data from XML. What I want is to be able to
declare a new instance of this class and make it a copy of the
existing instance. This way I can spawn various instances with their
own updated properties

The code "Dim NewInst as MyClass = OldInst" creates a reference to the
existing instance and, therefore, changing any properties of NewInst
are reflected in OldInst.

Is there a simple way to do what I need?

You need to have your object implement IClonable and use the .Clone method
to get a copy of your object. You will be responsible for doing the cloan,
either with a serialize dehydrate/hydrate mechanism or manual property setting.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
C

Chris Pratt

Thanks Chris, that was really helpful. It allowed me to write a generic
function which I can use for all cases, as below:
Public Sub CopyObject(ByVal ObjectType As Type, _
ByVal ExistingInstance As Object, _
ByRef NewInstance As Object)

Dim Props() As Reflection.PropertyInfo = _
ObjectType.GetProperties(Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.Instance)

For Each PropItem As Reflection.PropertyInfo In Props

If PropItem.CanWrite Then
PropItem.SetValue(NewInstance, _
PropItem.GetValue(ExistingInstance, Nothing), Nothing)
End If

Next

End Sub

Incidentally, what does the Reader object refer to in your example? Is this
a better way of extracting the property values than the way I have done it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top