Access and Assign Methods

Microsoft Visual FoxPro supports Access and Assign methods. These user-defined methods make it possible for you to execute code when querying the value of a property, or when you attempt to change the property's value.

Visual FoxPro executes the code in an Access method when querying the value of a property, typically by using the property in an object reference, storing the value of the property to a variable, or displaying the value of property with a question mark (?).

Visual FoxPro executes the code in an Assign method when you attempt to change the value of a property, typically by using the STORE or = command to assign a new value to the property.

Visual FoxPro executes Access and Assign methods when querying or changing property values only at run time, not design time.

Note   When attempting to assign a value to a property at run time, you must include a PARAMETERS or LPARAMETERS statement in the Assign method to accept the value because Visual FoxPro passes the value to the Assign method.

You can create Access and Assign methods independently of each other, that is, you can create an Access method without an Assign method, or an Assign method without an Access method. You can create Access and Assign methods for properties that are created programmatically within a DEFINE CLASS statement or interactively for a form or class with the Form and Class designers.

Note   You can create Access and Assign methods for all native Visual FoxPro properties. For example, you can create an Access method for the Left property of a form, making it possible for you to execute code whenever the form's Left property is queried. You can create an Assign method for a native Visual FoxPro property that is read-only, for example, the ParentClass property. However, the method is never executed.

Visual FoxPro treats Access and Assign methods as Protected at run time, so they cannot be accessed outside of the class definition. However, Visual FoxPro treats these methods in a special way in the Class Designer. Usually, Visual FoxPro marks the Protected method of an object dropped onto a container, for example, a command button dropped onto a form, as read-only and not editable in the designer. However, you can edit the Access and Assign methods of objects in the designer.

Note   The Assign method is not supported for the Value property of a control.

Benefits of Access and Assign Methods

Access and Assign methods provide the following benefits:

  • You can create a public interface for a class or object that separates the interface from the implementation.
  • You can easily implement property validation.
  • You can easily protect properties in subclassed ActiveX controls.

THIS_ACCESS Method

The code in a THIS_ACCESS method is executed whenever you attempt to change the value of a member of an object or a member of an object is queried.

A THIS_ACCESS method is created in code within a DEFINE CLASS command, or in the New Method or Edit Properties dialog boxes for .vcx visual class libraries. A THIS_ACCESS method must return an object reference always; otherwise, an error is generated. Typically, the THIS object reference is returned. A THIS_ACCESS method must also include a parameter to accept the name of the member of the object that is changed or queried.

The following simple example demonstrates how to create a THIS_ACCESS method in code within a DEFINE CLASS command. When this example is run as a program, 'Caption' is displayed twice, first when the Caption property is assigned a value, and again when the Caption property value is queried. The value of the Caption property ('abc') is then displayed.

CLEAR
oTempObj = CREATEOBJECT('MyForm')  && Instantiate the Form
oTempObj.Caption = 'abc'  && Assign a value, triggers THIS_ACCESS
? oTempObj.Caption  && Query a value, triggers THIS_ACCESS

DEFINE CLASS MyForm AS Form
   PROCEDURE THIS_ACCESS
      LPARAMETER cMemberName  && Object member name

      IF cMemberName = 'caption'
         ? cMemberName  && Display the object member name
      ENDIF
      RETURN THIS
   ENDPROC
ENDDEFINE

Note that THIS_ACCESS is not intended to be a global replacement for Access and Assign methods - it only provides information about which object member is accessed or queried. Unlike an Access or Assign method, THIS_ACCESS does not provide control over values returned to specific object members.

See Also

Object-Oriented Programming | Classes in Visual FoxPro | Calling Methods | Creating Access and Assign Methods