Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VARTYPE() error - rare quirk or a bug?

Status
Not open for further replies.

dbMark

Programmer
Apr 10, 2003
1,515
US
I was testing my code and encountered unexpected errors using VFP 9.0 sp1 function VARTYPE(). Normally it will always return the data type of the field or "U" if it does not exist.

But an error will be generated if the code references both a table and field where either the table/alias is not open or field does not exist.

That was a surprise to me. I often use VARTYPE() rather than TYPE() since it is claimed to be faster. I do not see a note about it in the Help. So is it a bug, an undocumented issue that should be listed in Help, or should I just use TYPE() whenever I'm referring to a field identified with its table/alias?

? VARTYPE(myTable.myField) && works as expected
? VARTYPE(NoTable.myField) && errors
? VARTYPE(myTable.NoField) && errors
? VARTYPE(NoTable.NoField) && errors
 
dbMark,

What error(s) do you get?

If I use VARTYPE referencing a unopened table I only get the error Alias is not found.

If I'm in an alias and use VARTYPE(ThisTable.NoField) I get the error Variable "NoField" is not found. Again, I guess I would expect that.

Stewart
 
Sorry, I forgot to explain myself adequately.

Microsoft's Help says this:
"VARTYPE( ) returns a single character indicating the data type of the expression."

I would have expected VARTYPE() to have returned the character "U" representing "Unknown or variable does not exist" as it does in other tests. It handles any type including "Unknown" - without errors - as long as the alias is not included.

? VARTYPE(myTable.myField) && returns type as expected
? VARTYPE(NoTable.myField) && error: Alias 'NoTable' is not found.
? VARTYPE(myTable.NoField) && error: Variable 'NoField' is not found.
? VARTYPE(NoTable.NoField) && error: Alias 'NoTable' is not found.

? VARTYPE(myField) && returns type as expected
? VARTYPE(NoField) && returns "U" as expected

? VARTYPE(myMemVar) && returns type as expected
? VARTYPE(NoMemVar) && returns "U" as expected

Frankly, I believe it is a bug since I do not trust a function which sometimes reports "U" for "Unknown" and other times errors. Isn't an aliased field an expression?
 
dbMark,

The errors here are, in a sense, not really from the VARTYPE function.

As I see it, VFP first looks for the opened table. The fact it can't find it, causes an error before it really gets to the VARTYPE function. This is demonstrated if you try ? NoTable.AnyField you again get the "Alias is not found" error.

If you reference an opened table, but a non-existent field with ? MyTable.NoField, I think VFP then treats the expression as a variable and so you get the "Var not found" error.

I think I'm right in saying that, when you are in a work area with an opened table, the line?VARTYPE(NoField) VARTYPE is actually checking for a variable called NoField not a field.

There is something about VFP looks first for a fieldname and then for a variable name. I'm sure that's in the help - maybe someone else knows where?


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Following-up - of course this is the situation where one person says "It's a bug" and another says "It's by design" !

Feel free to continue the dialog....

Stewart
 
Actually, it has to error because it evaluates its parameter before the function is called.

Tamar
 
Okay, I give up. I have verified a similar behavior pattern exists with TYPE(). Without the alias, no errors. With an alias, if the table or field does not exist, it errors just like VARTYPE(). Not surprising since VARTYPE is a subset of TYPE(). So I agree that errors are possible in certain circumstances with either function.
 
Tamar, not quite true, as I see it, but I do agree errors can happen.

? VARTYPE(noVar) && no error generated
U
 
When VARTYPE() was first added, it did error on any non-existing item. In a later version, the team added support for the "U" return value, but not for drilling down a table or object reference and getting "U" back.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top