How to: Create Access and Assign Methods

You can create Access and Assign methods for new custom properties, native Visual FoxPro properties, or existing custom properties. You can create them when adding or editing custom properties. For more information about Access and Assign methods, see Access and Assign Methods. For information about adding custom properties to forms, form sets, and classes, see How to: Add Properties and Methods to a Form and How to: Add Properties to Classes.

You can create Access and Assign methods interactively in the Form Designer or Class Designer. To create Access and Assign methods for properties of forms and form sets, use the Form Designer. To create Access and Assign methods for properties of classes for controls and other objects, use the Class Designer. You can also create Access and Assign methods programmatically.

To create an Access or Assign method when adding a new property

  1. In the New Property dialog box, select the Access Method box, the Assign Method box, or both.

  2. When you are finished creating the property, click Add.

When you create an Access or Assign method, the new method is added to the end of the properties list in the Properties window and appears with the suffix, _access or _assign.

For more information, see New Property Dialog Box.

To create an Access or Assign method for a native Visual FoxPro property

  1. If you are in the Form Designer, on the Form menu, choose New Method.

    -OR-

    If you are in the Class Designer, on the Class menu, choose New Method.

  2. In the Name box of the New Method dialog box, type the name of the native Visual FoxPro property followed with the suffix _ACCESS or _ASSIGN.

  3. When you are finished creating the method, click Add.

For more information, see New Method Dialog Box

To create an Access or Assign method for existing custom properties

  1. If you are in the Form Designer, on the Form menu, choose Edit Property/Method.

    -OR-

    If you are in the Class Designer, on the Class menu, choose Edit Property/Method.

  2. In the Edit Property/Method dialog box, select the property you want.

  3. Select the Access Method box, the Assign Method box, or both.

  4. Click Apply, and then Close.

For more information, see Edit Property/Method Dialog Box.

When you create an Access or Assign method for an existing custom property, the new method is added to the end of the properties list in the Properties window and appears with the suffix, _access or _assign.

To create Access and Assign methods programmatically

  • Use the DEFINE CLASS command and include the PROCEDURE clause.

Note

In Assign methods, you must include a PARAMETERS or LPARAMETERS statement so that when you attempt to assign a value to a property at run time, Visual FoxPro can accept the value and pass it to the Assign method.

For more information, see DEFINE CLASS Command.

For example, the following code creates a class named MyClass with Access and Assign methods called MyProperty_ACCESS and MyProperty_ASSIGN for the custom property MyProperty. MyProperty_ACCESS responds to queries on the value of MyProperty, while MyProperty_ASSIGN responds to changes in the value of MyProperty.

Note

The Assign method includes an LPARAMETERS statement so it can accept the value that is passed to it.

DEFINE CLASS MyClass AS Custom
   MyProperty = 100 

   PROCEDURE MyProperty_ACCESS 
      WAIT WINDOW 'This is the Access method';
         +  ' ' + PROGRAM( )
      RETURN THIS.MyProperty
   ENDPROC
   
   PROCEDURE MyProperty_ASSIGN 
      LPARAMETERS tAssign  
      WAIT WINDOW 'This is the Assign method';
          + ' ' + PROGRAM( )
   ENDPROC
ENDDEFINE

The following code example creates a Form class named frmMyForm with an Assign method called Left_ASSIGN for the form's native Left property. Left_ASSIGN performs simple validation on the property value and runs when an attempt is made to assign a value to the property. If you attempt to change the Left property to a negative value, the Assign method displays a message and leaves the value unchanged. If you attempt to change the Left property to a nonnegative value, the method sets the property to the specified value.

Note

The Assign method includes an LPARAMETERS statement so it can accept the value that is passed to it.

DEFINE CLASS frmMyForm AS Form
   PROCEDURE Left_ASSIGN 
      LPARAMETERS tAssign  
      DO CASE
         CASE tAssign < 0 && Value passed is negative.
            WAIT WINDOW 'Value must be greater than 0'
         OTHERWISE  && Value passed is not negative.
            THIS.Left = tAssign
      ENDCASE
   ENDPROC
ENDDEFINE

Creating THIS_ACCESS Methods

You can create THIS_ACCESS methods from the Form Designer, Class Designer, or programmatically.

To create a THIS_ACCESS method

  1. On the Form menu in the Form Designer or on the Class Menu in the Class Designer, choose New Method.

  2. In the New Method dialog box, type THIS_ACCESS.

  3. Click Add.

    When created, the THIS_ACCESS method appears in the Properties window. To add code to the method, double-click the method in the Properties window.

To create THIS_ACCESS methods programmatically

  • Use the DEFINE CLASS command and include the THIS_ACCESS keyword.

For more information, see DEFINE CLASS Command.

The following code example creates a Form class named MyForm with a THIS_ACCESS method, which contains an LPARAMETER statement with an object member name and some code to execute. The first line of code creates a form named oTempForm using the CREATEOBJECT( ) function. The second line of code attempts to assign a value to the form's Caption property. This action executes the THIS_ACCESS method and passes the name of the Caption property to the method. The THIS_ACCESS method displays the name of the object member, or 'Caption' in this example, using the question mark (?) command, and then returns an object reference for the form.

The third line attempts to display the value of the form's Caption property using the ? command. This action executes THIS_ACCESS again, passes the name of the Caption property to the method, displays 'Caption', and returns an object reference for the form. The value of the Caption property, 'abc', is then finally displayed.

oTempForm = CREATEOBJECT('MyForm')  
oTempForm.Caption = 'abc'  
? oTempForm.Caption  

DEFINE CLASS MyForm AS Form
   PROCEDURE THIS_ACCESS
      LPARAMETER cMemberName  

      IF cMemberName = 'caption'
         ? cMemberName  
      ENDIF
      RETURN THIS
   ENDPROC
ENDDEFINE

For more information, see DEFINE CLASS Command.

See Also

Tasks

How to: Add Properties to Classes

Concepts

Working with Classes in Visual FoxPro