Creating a new instance

F

ffa

Hi Guys

If I do something like:

Dim newClass As Customer
newClass = oCustomer (an existing Customer object)

Then when I make a change in oCustomer, the change is also applied to
newClass.

Is there some quick way to create a new instance of oCustomer that is an
independant object. I want to use this for some undo functionallity and
change logging.

Thanks
 
G

Guest

I don't know if this is necessarily the quickest way. I usually implement
ICloneable, and in the Clone method, serialize the current object then
deserialize it to anothier instance to return from the method. This would
only be for fairly complicated classes though, as if there are only a few
properties, its not worth it.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Hi Guys

If I do something like:

Dim newClass As Customer
newClass = oCustomer (an existing Customer object)

Then when I make a change in oCustomer, the change is also applied to
newClass.

Is there some quick way to create a new instance of oCustomer that is an
independant object. I want to use this for some undo functionallity and
change logging.

Thanks

There is no quick way to copy an object, as an object can be too
complicated for an automatic copy. It's up to the object itself to
implement cloning.

This can be done by implementing the IClonable interface, as
ModelBuilder suggested. Another common way of doing it is to make a
constructor that takes an object as parameter, so that you would do:

Dim newClass as New Customer(oCustomer)

The constructor would just copy the relevant data from the passed object
to make itself a copy of 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

Similar Threads


Top