Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to check the type of a parameter
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00508539
Message ID:
00508549
Views:
15
Shane --

There are 2 commonly used functions to determine a variable's type.
aVar = aVal

cType = TYPE("aVar"), or 
cType = VARTYPE (aVar) 
Do note the use of quotes around the variable name for TYPE and the lack for VARTYPE.

VARTYPE was introduced in VFP 5 and offers significant performance advantages over TYPE() (between 3 to 6 times). However, TYPE is a bit safer. If VARTYPE receives a value that can't be evaluated, you'll receive an error.

For the simple case, you could just select which native function to use.

Now, to determine if a string variable contains a variable name:

Consider the following:
X = "Y"      &&  Assign the value "Y" to the variable X
Z = "X"      &&  Assign the value "X" to the variable Z 

lcTypeofVariable = TYPE ("X")     &&  Returns "C" as we'd expect.
lcContainedVariable = EVALUATE (X) && Gives us an error message "Variable 'Y' not found."

*  So, the value of X, which is Y, is evaluated as a variable, which we haven't assigned.

*  Now let's take a look at what happens when we have assigned an underlying value.

lcTypeofVariable = TYPE ("Z")     &&  Returns "C" -- as we'd expect.
lcContainedVariableValue = EVALUATE (Z) && will contain "Y"
lcTypeofContainedVariable = TYPE ("lcContainedVariable")   && Returns "C"

* Because TYPE does an EVALUATE, we can actually shorten this as follows:
lcTypeofContainedVariable = TYPE (Z)  && Note, without quotes

*  This last format is particularly useful. If TYPE can't evaluate a value, it returns "U" for unknown, thus avoiding an error.

*  So, your routine could look like this:

FUNCTION   GetType
PARAMETER          tcVariable
LOCAL              lcType
   
   lcType = TYPE ("tcVariable")
   IF lcType = "C"
      IF TYPE (tcVariable) <> "U"   &&  a variable could refer to any type
           lcType = "V"             &&  create our own type value for "variable"
      ENDIF
   ENDIF

   RETURN   lcType
ENDFUNC
Enjoy!

Jay
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform