Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Vartype() bug in VFP 7.0?
Message
General information
Forum:
Visual FoxPro
Category:
Troubleshooting
Miscellaneous
Thread ID:
00825419
Message ID:
00825893
Views:
29
Rich,

>I have run into this problem with vartype() and properties which may contain object references or NULLs.

Let me start off, much to Fabio's dismay I'm sure *bg*, this is not a bug, it's a misuse of the function.

Here's a simpler example to illustrate what's going on:

ox = createobject( "textbox" )

the above line creates an object outside any containership hierarchy, therefore its .Parent property does not contain a valid object reference

_screen.AddObject( "test", "textbox" )

this creates the textbox as a contained object of _screen, so by definition its .Parent refers to _screen

? PEMSTATUS( ox, "parent", 5 )

this line returns a .T. of course because textbox has a parent property in its defined interface, but this does not mean that the property contains a valid object reference

? PEMSTATUS( _screen.test, "parent", 5 ) && .t.

? TYPE( "ox.parent" )

this returns U because the property does not have a valid value because it exists outside a containership hierarchy.

? type( "_screen.test.parent" ) && returns O because it is contained by _screen

Now before we go farther, open the locals window and look at ox, you'll see that its current value is (none) and its type is empty. This is because it exists outside any containership hierarchy.

If you try to do something like:

? vartype( ox.parent.BaseClass )

You'll get the "Parent is not an object" this is because it is forcing vartype to resolve an object reference (ox.parent) to get to the BaseClass property, which it can't do it because ox.parent isn't valid.

? type( "ox.parent.BaseClass" )

will return U because type is not working under the same principles that vartype requires to be true in order to work faster than type() can work.

If you want your class to both run inside and outside containership you'll have to code it this way:
if ( ( type( "this.parent.parent" ) = "O" ) )
   * contained
else
   * free running instance
endif
This topic ought to be better discussed in the documentation, but it's not a bug in the vartype() function itself.

>HackFox mentions that you shouldn't use vartype() to check for the existence of a variable, because it will crash with "variable not found." This is NOT my situation. I have checked that the property exists, now I'm trying to determine its type!
>
>I have a class which usually appears on a page of a pageframe class with certain other pages. I want to check if I have that situation, then query certain properties on another page of the parent pageframe. (I know I just broke 3 or 4 OOP design rules.)
>
>I test that pemstatus(this.parent, "parent", 5) = .T., so I know the property exists. Then I test vartype(this.parent.parent) to see whether it's an object (or NULL, presumably).
>
>Instead of returning either "U" for undefined or "X" for null as I would expect, I CRASH with "Parent is not an object."
>
>According to VFP docs <g>, and my past experience through VFP 6, vartype should return "U" if a variable/property is undefined, "X" if it's null, or the type (e.g. "O" if it's an object reference.)
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform