Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cloning an Object?

Status
Not open for further replies.

TheVampire

Programmer
May 1, 2002
828
US
I want to create a clone of an object, for instance, a class that has 30 properties. Am I forced to write a clone function for that class that creates a new class object, sets all of the properties and then returns the new class object to the caller. Or is there some way, perhaps using copymemory API, that I can write a shortcut that will create a clone of any class? I know pure VB6 does not have a way to do it, and that ByVal ( instead of ByRef ) does not work on objects, but I was hoping that the API would allow me to do this.

Thanks,

Robert
 
You ever have a problem that you can't figure out, and right after you finally ask a question about it the answer wacks you in the head?

Well, in this case the answer is the PropertyBag. Save the first class object into a PropertyBag, and then create a new instance of the object and set it with the PropertyBag that was saved from the first one. And you have a Clone!

Now I just have to hope that the government doesn't make this kind of cloning illegal...

Robert
 
But be aware that cloning objects can be fraught with unforeseen perils - particularly if an object holds a public or private reference to another object; do you clone the referenced object as well? Or do you create a new instance of the referenced object?
 
Nice to see you are keeping an eye on us, even while sitting in the sun on a beach. :)

Indeed, you would have to watch out for such perils. In my case, some of the objects I am cloning do indeed have references to other objects. The cloning does not create a new instance in my case, but adds to the counter of references to that object.

I'm being very careful to clean up after myself when I do this cloning.

Robert
 
"I'm being very careful to clean up after myself when I do this cloning." - always a good idea. Bury the evidence...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top