次の方法で共有


Access and Assign Methods

Microsoft® Visual FoxPro® has been enhanced to support Access and Assign methods. These user-defined methods make it possible for you to execute code when the value of a property is queried, or when you attempt to change the property's value.

The code in an Access method is executed when the value of a property is queried, 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 (?).

The code in an Assign method is executed 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.

Access and Assign methods are executed when property values are queried or changed at run time only. Querying or changing property values at design time doesn't execute Access and Assign methods.

Note   Because the value you attempt to assign to the property is passed to the Assign method, you must include a PARAMETERS or LPARAMETERS statement in the Assign method to accept the value.

Access and Assign methods can be created independently — you can create an Access method without an Assign method, or an Assign method without an Access method.

Access and Assign methods can be created for properties created programmatically within a DEFINE CLASS statement, or interactively for a form or class with the Form and Class designers. Access and Assign methods

Note   Access and Assign methods also can be created 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), but the method will never be executed.

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

A new global class method, THIS_ACCESS, has been added to Visual FoxPro. 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