Solved

Cloning objects

Posted on 2000-02-15
8
290 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

630 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