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

Creating a new instance

P: n/a
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
Jul 22 '07 #1
Share this Question
Share on Google+
2 Replies


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

"ff*@newsgroup.nospam" wrote:
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
Jul 23 '07 #2

P: n/a
ff*@newsgroup.nospam wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jul 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.