|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Copy of class instanceI'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. Chris Pratt wrote:
Show quoteHide quote > I'm sorry to ask such a fundamental question, but is it possible to create a I don't know of a way to automatically do this, but you really need to > 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. > > 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 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
Show quote
Hide quote
> I'm sorry to ask such a fundamental question, but is it possible to You need to have your object implement IClonable and use the .Clone method > 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? > 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
if then endif
Rank newbie question - Class or Function? Removing multiple items from a multi-select ListView how to speed up collections iteration VB VStudio 2003 and 2005 on the same workstation? How to access and use Unmanaged Code Disposing unused sockets Updating DataTable bound to a DataGridView I cannot add an item if no item exists before |
|||||||||||||||||||||||