Solved

Cloning objects

Posted on 2000-02-15
8
286 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 281
Dev express lookupcombo 3 64
Set WorkSheet  not Working 9 62
Delphi Firemonkey : user drawing in window 3 58
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

752 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