Condividi tramite


Access and Assign Methods

Visual FoxPro supports Access and Assign methods, which are user-defined procedures or functions that have the same name as a class property and have the suffix _ACCESS or _ASSIGN appended to the procedure or function name. You can use Access and Assign methods to execute code when querying the value of a property or attempting to change the property's value. Visual FoxPro executes Access and Assign methods only when querying or changing property values at run time, not design time. You can create Access and Assign methods separately and independently of each other.

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.

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

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

Note

You can create Access and Assign methods for most native Visual FoxPro properties. You can create Assign methods for properties that are read-only; however, the method never executes. Visual FoxPro does not support the Assign method for the Value property for controls nor does it support Access and Assign methods for native properties, events, or methods of ActiveX controls. However, Visual FoxPro supports Access and Assign methods for properties, events, and methods of the Visual FoxPro OLE Container in which an ActiveX control is contained.

Note

Access and Assign methods for member arrays do not fire when accessing the array using a native array function such as ASCAN().

Note

The Assign method will fire for certain native properties when their value is queried and not changed. These include dimensional properties such as Top, Left, Height, Width as well as a few others like Visible. This is due to how Visual FoxPro handles these properties internally.

Visual FoxPro treats Access and Assign methods as Protected at run time, so they cannot be accessed outside of the class definition. However, when you are in the Class Designer, Visual FoxPro treats these Access and Assign methods in a special way. When you drop an object onto a container, such as a command button onto a form, Visual FoxPro usually marks Protected methods of such objects as read-only and not modifiable in the designer. However, you can edit Access and Assign methods of such objects in the Class Designer.

THIS_ACCESS Methods

You can create THIS_ACCESS methods to execute code when changing the value for an object member or querying the object member. The THIS_ACCESS method must always return an object reference; otherwise, Visual FoxPro generates an error. Typically, the method returns the object reference, THIS. The THIS_ACCESS method must also include a parameter to accept the name of the object member that is changed or queried.

Note

THIS_ACCESS is not intended as a global replacement for Access and Assign methods. It only provides information about the object member that 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

Tasks

How to: Create Access and Assign Methods

Concepts

Working with Classes in Visual FoxPro