Solved

Cloning objects

Posted on 2000-02-15
8
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2522540
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
ID: 2522596
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
ID: 2522631
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:macu
ID: 2522650
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
 

Author Comment

by:Brian_McGee
ID: 2523042
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
ID: 2523169
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
ID: 2523194
"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
ID: 2523329
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

735 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