Microsoft KB Archive/208212

= PRB: Error When Using Assign Method to Assign Object References =

Article ID: 208212

Article Last Modified on 11/18/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q208212



SYMPTOMS
The following error message appears when using an assign method to store object references in a property and then trying to access properties of that object through the property:

Unknown member



CAUSE
The assign method fires twice when assigning an object reference to a property. The first time it fires, it sets the value of the property containing the object reference to NULL and then it runs again and assigns the property the object reference.



RESOLUTION
In the assign method, check to see if the new value is not NULL before assigning it to the property.

In the program code in the "Steps to Reproduce Behavior" section below, look in the class definition of the Test class and uncomment the IF...ENDIF block code as shown to check the new value. It is in the Procedure obj_assign: *  IF !ISNULL(m.vNewVal)        && uncomment this to make things work. THIS.obj    = m.vNewVal
 * ENDIF



Steps to Reproduce Behavior
The code in the steps below defines a class named Test that has two properties, obj and prevobj. The obj property has an assign method on it. The assign method takes the value in the obj property and assigns it to prevobj before the new value of obj is assigned.

The code in the program creates an object reference to the Test class and then instantiates an instance of a class based on the custom base class and assigns this object reference to the obj property. It does this three times. When the reference to the custom class is assigned to the obj property, the assign method fires.

The code outputs with ? commands to the Visual FoxPro desktop the values in the obj and prevobj properties at various times. Note that the ? command in the assign method fires twice the second and third times the new object reference to the custom class is assigned to the obj property.

The error message

Unknown member PREVOBJ.

appears the third time when oTest.prevobj is not checked to see if it is NULL before accessing the oTest.prevobj.name property.   Copy the following code into a new program: CLEAR

LOCAL oTest, oCustom oTest = CREATEOBJECT("test")

WITH oTest oCustom  = NEWOBJECT("custom") .obj = oCustom .obj.NAME = "one" ? "obj    = ", .obj.NAME * oTest.prevobj is null the first time through. ? "prevobj = ", IIF(ISNULL(.prevobj),"Null", .prevobj.NAME) ?

RELEASE oCustom ? "Second Code Block" oCustom = NEWOBJECT("custom") .obj = oCustom .obj.NAME = "two" ? "obj = ", .obj.NAME ? "prevobj = ", IIF(ISNULL(.prevobj),"Null", .prevobj.NAME) ?

RELEASE oCustom ? "Third Code Block" oCustom = NEWOBJECT("custom") .obj = oCustom .obj.NAME = "three" ? "obj = ", .obj.NAME ? "prevobj = ", IIF(ISNULL(.prevobj),"Null", .prevobj.NAME) ? "prevobj = ", .prevobj.NAME  && this causes error ? ENDWITH

oTest = NULL oCustom = NULL

RETURN

DEFINE CLASS test AS CUSTOM obj    = NULL prevobj = NULL

PROCEDURE obj_assign LPARAMETERS vNewVal THIS.prevobj = THIS.obj ? "Assign Method: The value of this.prevobj is " + IIF(ISNULL(THIS.prevobj), 'Null', 'Not Null')

THIS.obj    = m.vNewVal ENDPROC ENDDEFINE  Save and run the code. Note the output on the Visual FoxPro desktop. Uncomment the commented IF...ENDIF block in the obj_assign procedure in the Test class definition and re-run the code. Note that this time the output in the second and third code blocks for the prevobj property is "one" and "two" instead of NULL. The error message does not appear either. The assign method only fires once when assigning other data types to a property. Code to demonstrate this is not included in this article.
 * IF !ISNULL(m.vNewVal)       && uncomment this to make things work.
 * ENDIF

Additional query words: kbDSE

Keywords: kboop kbcodesnippet kbprb kbpending KB208212

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.