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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make an object's copy on a new memory location?

Status
Not open for further replies.

Rissole

MIS
Jul 10, 2002
21
US
Hello,

I'm working with Visual Basic 6. I need to make a copy of an object but to a fresh memory location.

In other words, I dont want to copy the reference but I need an identical object on isolated location.

Thanks in advance,
Rissole.
 
Ah - you want cloning! I wrote a longish note about this in this forum some time ago. Unfortunately, the search engine does not appear to working properly at the moment, so I can't find the necessary link.

In summary, effective cloning of COM objects is tricky, even though it sounds like it should be easy. But there are lots of complications (e.g what if the object contains a reference to another object: do you need to clone that referenced object as well? Or, a simpler example, what if the object contains a handle to a bitmap: will you need to make a copy of that bitmap and return thus generate a new handle?)

Anyway, as a result VB has no general Clone method, although there are one or two objects that DO have a Clone method, eg the Font object (if you use the IFont interface). Additionally, some objects are what is know as self-persisting (eg the Picture object), which means that you can clone them via writing to and then reading from a PropertyBag)

In general, however, you'd have to write your own cloning method. A generic cloning method would require you to be able to enumerate all the properties of the current object, identify their current value, and then write them to the properties of a new, empty object. Of course, even this has problems: some of the properties of an object might be Get but not Let - so how would you write them?

Just for fun, here's the ProertyBag nmethod of cloning a Picture:
[tt]
Public Function ClonePicture(srcPicture As Picture) As Picture
With New PropertyBag
.WriteProperty "temp", srcPicture
Set ClonePicture = .ReadProperty("temp")
End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top