Solved

Cloning objects

Posted on 2000-02-15
8
250 Views
Last Modified: 2013-11-23
Is there an API or other method somehow to clone objects in VB?

Dim objClient As CClient
Set objClient = frmSomething.Client

....and now you want to clear frmSomething.Client, but of course if you set it to nothing, objClient will be nothing as well due to the fact that the Set statement creates a pointer to frmSomething.Client and not a clone of it.

How do you create a clone? (other than declaring a new object variable and manually assigning each property with a let statement!)
0
Comment
Question by:Brian_McGee
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
In VB5, if you want a new object, the original object needs to be defined with an index...  For example, add a textbox and set the Index property to 0.

Then you can do:  Load Text1(1)

and you will get another textbox with an index of 1...


Cheers!
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
You could use the CopyMemory API call, but the trick is to figure out what the size of the class object is in bytes.  LenB only works on user-defined types and not classes.
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 100 total points
Comment Utility
Yeah, CopyMemory won't work with classes, except, maybe, just very very maybe, in the most stringently restricted cases. All but useless.

No, Brian, the plain and simple truth is that you cannot clone objects with a nice 'n easy function call. You'll have to write a tailor-made cloning procedure for each class you wish to clone.
0
 
LVL 1

Expert Comment

by:macu
Comment Utility
I'm pretty sure you just have to do it all yourself, I guess you'd want syntax like Set NewObject = ObjectToClone.Clone. Also, take a look at:

http://www.themandelbrotset.com/html/wp.html

And read the paper on constructors in VB, using this approach may be something like what you want.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Brian_McGee
Comment Utility
If you could loop through the properties in an object you could write a generic function to clone classes.... though I suppose that's probably not possible!
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Actually, you can get the properties of an object... See http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10293195  


Cheers!
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
"could write a generic function to clone classes"

No, not even then. Well... Yes... If your properties are only simple data types. But what if your properties are themselves objects? Then you'll have to clone them too. And what if they then have properties that are also objects....

etc, etc, etc,...
0
 
LVL 1

Expert Comment

by:macu
Comment Utility
The code to get all properties of an object is good if you only want the names of properties, to get the values you need to get specific to the object. So I think the simplest way is to just have a Clone method on every object you want to clone. Each object will also know if it has any child objects that need cloning.

Something like:

Public Function Clone() As ObjectOfMyType

Dim oTemp As ObjectOfMyType

Set oTemp = New ObjectOfMyType

With oTemp

    'copy all properties to temp object
    .PropertyX = Me.PropertyX
    .PropertyY = Me.PropertyY
    ...
    'property is an object
    Set .ObjectProperty = Me.ObjectProperty.Clone
   
End With

'return copy of Me
Set Clone = oTemp

....

In case you don't know if child objects can be cloned, you could have an IClone interface, containing the clone function (and perhaps other stuff?) Then replace the above line with:

If TypeOf .ObjectProperty Is IClone Then Set .ObjectProperty = Me.ObjectProperty.Clone
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now