Wiki Home

Access And Assign Methods

Namespace: VFP
All About Access And Assign Methods

To get control ("hook" in) when the value of a property of your class is written to or read from, simply create a method in the same class that has the same name as the property, and add to the end of the method name "_ASSIGN" or "_ACCESS". In the Class Designer you simply check on the Access Assign when you create the property. The method propertyname_access is called whenever the value of the property is used. The method propertyname_assign is called (with the new value as the only parameter, usually. See Access And Assign With Arrays) any time code attempts to change the value of the property. If you want the value of the property to change, you must assign the value inside your method.

  MyProperty = 0
  PROCEDURE MyProperty_Access
    if Day(Date())%2=0
      * Act differently on odd dates.
      RETURN 1
      RETURN THIS.MyProperty
  PROCEDURE MyProperty_Assign( vNewVal )
    if VarType(vNewVal)='N'
      THIS.MyProperty = vNewVal

Things to be careful about

Problems with Access and Assign Methods

Occasionally, when I'm using Access or Assign Methods, the value of THIS will point to the wrong object. I'm not sure if it's always the same circumstances, but one situation is when the Control Source of a Text Box was set to "" and the code that follows is the definition of the class of "MyObject".
DEFINE CLASS clsMyObject AS custom
  oRecord = .NULL.
  Desc    = ''
    USE mytable
    Seek (pItemID)
    * I verified, there IS a field "Desc" in "mytable" (memo field)
    *  and the field IS scattered into the oRecord Object
  PROCEDURE Desc_Access
    * This next line blows up when a textbox.ControlSource is set
    *   to point to the Desc property of an object of class clsMyObject
    *   with the error "'Desc' is not a member".  Suspending and tracing
    *   through this line reveals that "THIS" here still refers to the
    *   textbox object.
    RETURN THIS.oRecord.Desc

* The following code is in the form.Init method
THISFORM.MyObject = CreateObject('clsMyObject',ItemID)
* The next line blows up with error "'Desc' is not a member".
*   Upon suspending, I've verified that "THIS" refers to the textbox
*   and not to the instance of MyObject
THISFORM.txtDesc.ControlSource = "THISFORM.MyObject.Desc"

Anyone have similar problems? Anyone find a solution (or an explanation)?
- ?wgcs
Okay, I found a workaround: In the _Access method I put:
IF Type('THIS.oRecord.Desc')='C'
  RETURN THIS.oRecord.Desc

As a result, everything works fine... and "VFP BUG" NEVER shows up in the text field, so everythings happy, but I don't like this solution because it seems frail... likely to bite me eventually. But I can't figure out another way except to give up the _access & _assigns.
- ?wgcs

Change the property name. DESC is a keyword in VFP used for sorting in descending order. When you use keywords, there is no telling what will happen. :-) -- Larry Miller

Okay, I don't have proof, but I know this isn't the only time I've encountered this bug and I'm almost certain the other times I wasn't using a reserved word for the property name. - ?wgcs

This bug appears to be fixed in VFP7. (Just beware of developing in VFP7 and running in VFP6!) Rhodri C Evans

VFP7 SP1 fixes a bug in VFP7 related to the Assign method. The bug is that the Assign method can leave a dangling reference when an object is assigned to a property. Since the object won't be released the form it is on won't be released either. -- Paul Grover

Category VFP Bugs Category Open Questions
Contributors: wgcs Larry Miller Rhodri C Evans
See Also: This_Access, DecoratingWithTHIS_ACCESS
Category Access And Assign
( Topic last updated: 2002.01.25 10:32:59 AM )