Wiki Home

Var Type

Namespace: WIN_COM_API
Var Type is a VFP function that acts similarly to the TYPE() function. The differences are
  • Var Type is much faster than TYPE()
  • TYPE() takes a string, while Var Type() takes the field, memvar, or property directly.
  • Var Type will error if it is passed the property of a nonexistent object.
  • Both return a single character for the data type, but Var Type() additionally returns "X" for .NULL.
    The differences in passed values is demonstrated below:

    Given that you have an object named oObj and you want to check the data type of a property of oObj named Myprop you would:

    Var Type(oobj.Myprop)

    - OR -


    Note the use of quotes in the Type call to make the property reference into an expression instead of a property name.
    Note on the above example;

    If object oObj does not exist:

    Var Type(oObj.MyProp) && will cause an error

    TYPE("oObj.MyProp") && will return U as undefined

    Type and Evaluate take the same form of a parameter; Evaluate returns the value, Type returns the type of the value. -- CFK

    Has anyone ever seen the TYPE() function return the value A? I just got an A as the return value and I have no idea what it can mean. -- Chad Bourque

    No, and neither the Hacker's Guide or the VFP Documentation mentions "A" as a possible return value. Is it possible for you to give any more information, such as a sample of the code that's producing "A" as a return? -- Peter Crabtree

    I think VFP9 returns A for an array. Could be wrong... just going from memory. -- Jim Nelson

    Nope, it doesn't, and I'm not suprised: that would break quite a bit of code. VFP9 returns the type of the first element in the array, just like previous versions. (At least, with the beta that I have) -- Peter Crabtree

    Just noticed this in the VFP9 help file.
    Include the optional parameter 1 to determine if cExpression is an array or a Collection Class. COM objects are not supported with the 1 parameter.
    If you give 1 as the second parameter to TYPE() it will return 'A' if it's an Array and 'U' otherwise.

    If you ask for VARTYPE(fieldname) and fieldname does not exist, then VARTYPE() returns "U".

    VARTYPE accepts .T. as a second argument to turn off the "X for null" feature. In other words, if you ask for VARTYPE(fieldname, .T.), and the field is a character field, VARTYPE() returns "C" regardless of whether the field is currently null.

    However, if you ask for VARTYPE(fieldname, .T.) and fieldname does not exist, then VARTYPE() bombs off with "Variable not found". It seems to me that any human would want VARTYPE() to return "U" in this scenario. -- KenReeder

    See also: VFP Data Types
    Category Learning VFP
  • ( Topic last updated: 2011.03.21 10:30:50 AM )