Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Fun Code: Cloning Objects!
Message
De
08/12/1997 19:25:16
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00064532
Message ID:
00064554
Vues:
34
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform