I have created a class module, and my program creates many instances of the class. Each object has a lot of data, and I want to be able to create a new instance that is a copy of an existing instance. I can't figure out a automatic way to do it with Visual Basic 6. In .Net I think there is some command called "DirectCast", but of course that is no help anyways.

Recommended Answers

All 9 Replies

You will need to serialize the object class and then consume the serialized data from the new object.

commented: ingenious idea +1

Thanks so much for the suggestion. This could certainly help get me out of a jam. I have never serialized an object before though. I'm starting a web search but so far haven't found much (for VB6). Any example lines of codes or anything?

Here is a MSDN article for you.

http://msdn.microsoft.com/en-us/library/aa263542(VS.60).aspx

See the topics:

1. Using the PropertyBag Object to Persist an Object
2. Depersisting An Object

You can use the code from the article without writing the contents to a file. Just pass the variable from object to object.

Actually, now that I've read some stuff, I'm not sure this would fix my problem. It's my fault for not being more clear. Basically, at certain points in the code I need to create a copy of my custom objects, which are rather complex (an "object tree" and all that). I want to do this automatically to the point where I do not have to explicitly mention the specific properties at all. The code gets updated a lot, and bugs are occurring when I change the structure or add properties to my objects, then fail to go back to where I'm making the copies and make the necessary changes. So it would be great if I could just tell Visual Basic to make a copy, WITHOUT needing to explicitly mention every single property.

But still, the serializing object suggestion was a good idea.. never would have thought of that.

(Edit: But maybe the links you sent will fix the problem and I don't see it... when I get to work tomorrow I'll play around with it.)

Unfortunately VB6 wasn't designed around that concept. Serialization is about the best you have when it comes to cloning a class object.

If the class stores all of its properties in a UDT, you can consider a "serialization" of the UDT passed via a Let/Get property, but this will not work if any of the properties contain object references (aka memory addresses).

Unfortunately the data is not in a UDT, but still another slick idea, thanks again. After this project is finished I am done with VB6 and moving to .NET or some other language, as the many gaps in VB6 (as an object-orientel language) are becoming a pain.

Very interesting, I look forward to trying that out. It looks straightforward, but as it is all completely new terrain for me, might have to to wait for the next project to learn it. Deadline in two days...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.