Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cloning objects

Posted on 2000-02-15
8
Medium Priority
?
292 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

688 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