By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Copy of class instance

P: n/a
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.
Jul 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris Pratt wrote:
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.Publ ic 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.Nam e)), Nothing)
Next
Jul 27 '06 #2

P: n/a
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
Jul 27 '06 #3

P: n/a
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.P ublic 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?

"Chris" <no@spam.comwrote in message
news:ub**************@TK2MSFTNGP04.phx.gbl...
Chris Pratt wrote:
>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.Publ ic 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.Nam e)), Nothing)
Next

Jul 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.