What’s the difference between Vartype and Type

To get the type of variable “MyVar”, you can use TYPE(“MyVar”) or VARTYPE(MyVar). Notice no quotes are used for VARTYPE.

TYPE(cExp) scans and compiles the expression in the string parameter. For example, for TYPE(“3+4”), VFP will compile the expression in the string parameter, evaluate it, then return the type of the result. Putting it in a loop causes the string to be compiled each time. It has been in the language since foxbase days

VARTYPE(eExp) will just return the type of the value at the top of the Fox expression stack. There is no extra compilation step and the expression is evaluated just as any VFP expression before the VARTYPE routine gets called to return the type of the value at the top of the stack. VARTYPE was added around VFP6.

I always thought TYPE was faster than VARTYPE, but I can’t remember why cause you’d think it would be slower. Also, FYI for other readers of this blog, TYPE will not throw an error if it doesn’t evaluate to an actual var (will return "U"), but VARTYPE will throw an error if var does not exist.

"Actually vartype only seems to run into problem with array members that don’t exist"

Whoa! You are so right! When did this change? I could have swore that VARTYPE used to blow up if the var didn’t exist. I have a lot of old code using TYPE vs. VARTYPE because I thought it was "safer" – usually it was to test for an object to be type "O" before calling PEMSTATUS, etc. Hmmm… Come to think of it, maybe I’m thinking of PEMSTATUS vs. TYPE on the performance thing too…

Interesting… I just did a code reference for TYPE(" vs. VARTYPE( in the latest Visual Fox Express (VFE2005) classes – interestingly, there are still 752 occurrences of TYPE() but there are 751 occurrences of VARTYPE() – WHY? Looking closer it seems mostly TYPE is used as an alternative to PEMSTATUS – obvioulsy, it’s got to be quicker to make one TYPE() call vs. testing for an object using TYPE() then checking for a property on that object using PEMSTATUS. But, if VARTYPE() doesn’t bomb out on an object that doesn’t "exist" wouldn’t that make the code much faster? I think I need to talk to the Feltmans about this…

Try the above code with and without the object existing. Bottomline, I’m going to be avoiding the use of TYPE() as often as I can now, especially where I know there will potentially be many calls to it.