Access, Assign
These methods, added in VFP 6, make it easier to write good OOP code. Every property of an object can have an Access method and an Assign method. The property's Access method fires whenever the property is read (accessed) while its Assign method fires whenever the property is changed (assigned).
Usage |
PROCEDURE property_Access PROCEDURE property_Assign LPARAMETERS uNewValue |
You can even create Access and Assign methods for built-in properties of classes. Use the Edit Property/Method dialog. To do this for forms (created with the Form Designer), you have to create the methods using New Method—the form's built-in properties don't show up in the Edit Property/Method dialog. That's probably because forms aren't really sure whether they're objects or classes. |
VFP 7 Service Pack 1 fixes a bug in the original release of VFP 7 (but not previous versions of VFP): Storing a reference to an object in a property of another object with an Assign method left a dangling reference to the first object (the one being stored). Dangling references are bad, because they prevent objects from being released properly (for example, forms won't close even when you click on the Close box). |
Example |
* Make BackColor read-only PROCEDURE backcolor_assign LPARAMETERS vNewVal * Throw away the new value RETURN * Limit a custom property to values between 1 and 10 PROCEDURE myproperty_assign LPARAMETERS vNewVal IF VARTYPE(vNewVal)<>"N" ERROR 1732 && "Data type invalid for this property" ELSE IF BETWEEN(vNewVal,1,10) This.MyProperty = m.vNewVal ELSE ERROR 46 && "Expression evaluated to an illegal value" ENDIF ENDIF RETURN * Make sure a property contains the right value when it's used. * In this case, perimeter is meant to be the perimeter of a * quadrilateral, so it should always be the sum of the * four sides. PROCEDURE perimeter_access This.Perimeter = This.Side1 + This.Side2 + ; This.Side3 + This.Side4 RETURN This.Perimeter * Demonstration of on-the-fly property creation using * This_Access oDear = CreateObject("cusTestAA") ? oDear.False && Displays .F. for the newly created property oDear.BackColor = RGB(255,255,255) ? oDear.BackColor && Shows the numeric equivalent of white release oDear return DEFINE CLASS cusTestAA as Custom PROCEDURE This_Access LPARAMETERS cMember * Determine if the member already exists IF PemStatus(This, cMember, 5) && Already exists, okay ELSE This.AddProperty(cMember) ENDIF RETURN This ENDDEFINE |