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.
DEFINE CLASS Sample AS CUSTOM
MyProperty = 0
* Act differently on odd dates.
PROCEDURE MyProperty_Assign( vNewVal )
THIS.MyProperty = vNewVal
Things to be careful about
- If, within an propertyname_assign method, you call another method in the same object, any assignments to the property "propertyname" will invoke another instance of the propertyname_assign method, ad infinitum.
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 "THISFORM.MyObject.property" and the code that follows is the definition of the class of "MyObject".
DEFINE CLASS clsMyObject AS custom
oRecord = .NULL.
Desc = ''
SCATTER NAME THIS.oRecord MEMO
* I verified, there IS a field "Desc" in "mytable" (memo field)
* and the field IS scattered into the oRecord Object
* 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.
* 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)?
Okay, I found a workaround: In the _Access method I put:
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.
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 )