Accessor Events

last modified: February 25, 2007

Accessor events are automatic and syntactically transparent accessor methods. For example, in VisualFoxPro 6, if a method named property_Assign() exists, this method fires automatically when property is modified. If a method named property_Access() is defined, this method fires automatically when property is read. The property_Assign() and property_Access() methods, if they exist, are responsible for completing the transaction and circularities are preempted by the class internals.

Moreover, if a method named THIS_Access() is defined, this method fires for every property read, every property write, and every method call. This_Access() should return an object instance (usually THIS) that will handle the message. Think of this as a pre-hook for all inbound messages destined to an instance.

An example, in VisualFoxPro 6 code:

x= CREATE("Foo")
x.Caption= "this is lowercase"
?x.Caption  && echoes "The Caption is: THIS IS LOWERCASE"
?x.Tag      && echoes "Instance B"

DEFINE CLASS Foo AS Label
  Caption= ""
  Tag= "Instance A"

  FUNCTION Caption_Assign( tcPassed)
    *-- Force caption to uppercase
    THIS.Caption= UPPER( tcPassed)

  FUNCTION Caption_Access
     RETURN "The Caption is: " + THIS.Caption

  FUNCTION THIS.Access()
    LOCAL oBar
    oBar= CREATE( "Foo")
    oBar.Tag= "Instance B"
    RETURN oBar

ENDDEFINE

Is this cool, or what? StevenBlack


Yes. I believe SatherLanguage (and CecilLanguage and probably others) are similar, and LispLanguage variants (CommonLispObjectSystem) go way back. Another approach is to use a MetaObjectProtocol to change what assignment etc mean for particular objects. -- DaveHarris


CategoryJargon CategoryEvents


Loading...