Home All Groups Group Topic Archive Search About

Copy of class instance

Author
27 Jul 2006 5:04 PM
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.

Author
27 Jul 2006 6:50 PM
Chris
Chris Pratt wrote:
Show quoteHide quote
> 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
Author
28 Jul 2006 7:48 AM
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?

Show quoteHide quote
"Chris" <no@spam.com> wrote in message
news:ubFB$1asGHA.4596@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.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
Author
27 Jul 2006 6:57 PM
Jim Wooley
Show quote Hide quote
> 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