Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Copy an Object
Message
From
16/07/2006 12:45:34
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
 
 
To
15/07/2006 18:49:14
John Tomblin
Service Station Systems, Inc.
San Jose, California, United States
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Environment versions
Visual FoxPro:
VFP 7
OS:
Windows XP
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01136724
Message ID:
01136792
Views:
13
>
>Object1 = CreateObject("Custom")
>Object1.AddProperyt("Test", "Green")
>Object 2 = Object1
>Object2.Test = "Red"
>?Object1.Test     && Returns Red not Green
>
>Object 2 = Object1   && apparently doesn't copy the object, it just gives it an additional name.
>
>
>How can I make an independant copy of Object1?

Only at design time, using obj.CloneObject("newname") in a builder. I've done a bunch of builders and never used this... just didn't find it useful.

I know it's not polite to ask "what do you need this for", because you probably have good reasons, but since creating a copy of an object is such a pain downstairs, we may help you achieve what you want by different means.

To copy an object you basically create another of the same class and classlib, then start copying any properties of the first object that are different from default. Amembers(aArray, oFirstObj,0, "C") would give you the list... something like this:
procedure copyobject(oObj, lcNewObjName)
loNewObj=newobject(oobj.class, oobj.classlibrary)
loNewObj.name=lcNewObjName
n=Amembers(aArray, oObj,0, "C")
for i=1 to n
   lonewobj.addproperty(aArray[i], getpem(oObj, aArray[i]))
endfor
return loNewObj
The problem with this is that some of the properties need to be skipped - for instance, they may be read-only, or protected, or hidden. Then, there's also the matter of the object's parent - do you want them to be child objects of the same parent? In that case, lines 2 and 3 are replaced with
oObj.parent.newobject(lcNewObjName, oobj.class, oobj.classlibrary)
and you may need to skip the name property in your loop.

Another problem here is what would you do if your object is a composite object - an optiongroup, a grid, a container...? Wouldn't you have to recurse and copy the member objects as well? And check for their readonly, protected etc properties?

So... I figure object copying is feasible, but it's so riddled with problems and trouble, that it's probably not worth it - there is usually a simpler way to do whatever you wanted to accomplish.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Reply
Map
View

Click here to load this message in the networking platform