Test if variable exists?

In Visual Foxpro there was a function called TYPE that you could use to test if a variable was initialized and what type it was. Is there a way to do this in XOJO without generating an exception?

For example:

? TYPE(“cVariable”)

Would return “U” if undefined
“C” if a character variable
“N” if a numeric variable


Why wouldn’t you know what type a variable is? You have to define them before you use them.

i guess that’s the difference between a strongly typed language and one that is interpreted where you can create variables of any type on the fly.

Believe me, in VFP, being able to create variables on the fly was extremely useful, albeit a bit dangerous if you weren’t careful.

Two things come close to this:

  • Use a Variant or Auto. You still have to know the variable name but you can test to see if it is null (not initialized). Use If MyVariant.IsNull or If MyAuto = nil.

  • Use a Dictionary and store your “variables” as keys. You can test if the “variable” is defined with MyDict.HasKey and if it’s been initialized as above.

I recommend neither of these. Take advantage of the strongly typed language.

surprised nobody had mentioned this already?

of course that only indicates the type, not that it exists…
but if it didn’t exist, it wouldn’t compile