Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Fun Code: Cloning Objects!
Message
 
À
08/12/1997 19:25:16
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00064532
Message ID:
00064628
Vues:
38
>>I had a need to clone objects on the fly. Here is the code I came up with...
>>To try this out, create a form and place a textbox on the form. Change the
>>textbox's DragMode property to Automatic. Then, in the DragDrop method of
>>the form, place the code found at the bottom of this message. Just run the
>>form and drag-and-drop the textbox somewhere on the form. The code will
>>duplicate the object with all properties , even custom properties that are
>>not part of the class. For example, type in some text in the textbox at
>>runtime, drag-and-drop it, the text is also duplicated. The cloned
>>textboxes can also be cloned ad infinitum. What is great about this example
>>is that it allows you to clone an object on the fly, regardless of if the
>>object is a foxpro base class or a custom object with custom properties set.
>>If anyone is interested, I was researching this to be used with my searchable
>>textbox class to make it more versatile and generic.
>>
>>
>>****************************
>>LPARAMETERS oSource, nXCoord, nYCoord
>>LOCAL laClone, laExclude, lnSize, lnExclude, lnFound, luValue, lcObjName
>>
>>** Create a public variable to hold the instance number.
>>** Faster than looking through objects for last instance number.
>>IF TYPE('gnInstance') <> 'N'
>> PUBLIC gnInstance
>> gnInstance = 0
>>ELSE
>> gnInstance=gnInstance+1
>>ENDIF
>>lcObjName = "txtDrag"+ALLTRIM(STR(gnInstance))
>>
>>lnExclude = 10
>>lnFound = 0
>>DIMENSION laClone(1), laExclude(lnExclude)
>>
>>** Set properties that are either read-only or that
>>** we do not wish to change from the default.
>>laExclude(1) = "CONTROLSOURCE"
>>laExclude(2) = "BASECLASS"
>>laExclude(3) = "CLASS"
>>laExclude(4) = "CLASSLIBRARY"
>>laExclude(5) = "NAME"
>>laExclude(6) = "PARENT"
>>laExclude(7) = "PARENTCLASS"
>>laExclude(8) = "TEXT"
>>laExclude(9) = "TOP"
>>laExclude(10) = "LEFT"
>>
>>lnSize=AMEMBERS(laClone, oSource)
>>
>>** Remove the properties we do not want to set
>>FOR i=1 TO lnExclude
>> lnFound = ASCAN(laClone, laExclude(i))
>> IF lnFound != 0
>> =ADEL(laClone, lnFound)
>> lnSize = lnSize - 1
>> lnFound = 0
>> ENDIF
>>ENDFOR
>>
>>THIS.ADDOBJECT(lcObjName, oSource.CLASS)
>>
>>** Set all the remaining properties
>>FOR i=1 TO lnSize
>> lcproperty = laClone(i)
>> luValue = oSource.&lcproperty
>> THIS.&lcObjName..&lcproperty = luValue
>>ENDFOR
>>
>>THIS.&lcObjName..LEFT = nXCoord
>>THIS.&lcObjName..TOP = nYCoord
>>****************************
>>

>
>This is interesting. I have some remarks&questions:
>1. The array properties will not be copied correctly.
>2. What's the use of the gnInstance?
>3. If a property keeps a reference to an object, you will have multiple references to the same object. This may cause problems when you release the form.
>4. How do you handle other read-only properties?
>5. You can use a
>WITH THIS.&lcObjName ... ENDWITH for the final part. It should be faster.
>
>Vlad


1. That is true. If you have an array property only the first value of the array will be copied. In my research,
however, I did not have a need for an array property, so I didn't consider it ;). However, placing some code
in the routine that checks for arrays should be easy enough.

2. I used gnInstance as an easy way to name cloned objects without having to go through the form's member list
and find a name that isn't used. Obviously, a better way would be to get the member list and do just that,
but once again for my purposes this wasn't necessary. This also would be a fairly small bit of code, easy
to impliment.

3. Yes, an object won't be released before the references to it are released. However, I believe that the
objects will be released from newest to oldest, so all the object references should be gone by the time
the object gets released. If there are problems with this, however, I would simply skip over properties
that are object references.

4. Ideally, to make this a generic as possible, you would check to see what type of control you are cloning
and then have an array with the excluded properties for that type of control. Then have common
excluded properties that would be used for all controls (for example, "BASECLASS" is read-only for all
controls).

5. Heh, would using "WITH THIS.&lcObjName" really be faster for two lines of code?


This was more of a mental excercise than anything else, however if anyone is interested in this I could
continue working out the above problems. Let me know.

Michael G. Emmons
memmons@nc.rr.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform