Type(), VarType()
TYPE() returns the data type of the variable or field passed to it. One of the most common errors in coding FoxPro has been to forget the quotes required around the parameter for the TYPE() function. VARTYPE(), introduced in VFP 6, doesn't require quotes, is significantly faster, and provides better reporting of whether the expression is null, but it has its own idiosyncrasies. Since neither function detects code page translation flags, nor can they distinguish the many numeric field types, sometimes AFIELDS() is far more useful.
Usage |
cValue = TYPE( cExpression ) cValue = VARTYPE( eExpression [, lIgnoreNull ] ) |
Parameter |
Value |
Meaning |
cExpression |
Character |
A string that can be evaluated to a legitimate variable or field name, or a valid expression. |
eExpression |
Expression |
Any expression. |
lIgnoreNull |
.F. or omitted |
Return "X" if eExpression is null. |
.T. |
Return the underlying object, field or variable type if eExpression is null. |
|
cValue |
Single character |
The type of the expression returned. |
Passing a variable of type "S" (see "SAVE SCREEN") to VARTYPE() results in error 19, "Data type mismatch." The TYPE() function properly returns "S". |
Returning the TYPE() or VARTYPE() of integer, numeric, double and float fields all as "N" is a bug. While all are treated the same way when used as memory variables, the storage and precision of integers and doubles are significantly different than the other two. A work-around is to use AFIELDS() to determine which field type you're working with, and reserve the use of TYPE() for memory variables. |
Example |
? TYPE("_VFP.Name") && Returns "C" oThing = CREATEOBJECT("Form") && Create a form object cName = "oThing" && Store the variable name ? TYPE("oThing") && Returns "O" ? TYPE(cName) && Also returns "O" ? ISNULL(oThing) && Returns .F. ? VARTYPE(oThing) && Returns "O" ? VARTYPE(cName) && cName returns "C" oThing.Release() && Make it go away ? TYPE("oThing") && Still "O" ? ISNULL(oThing) && Now .T. ? VARTYPE(oThing) && Now "X" |
See Also |