Xbasic

variable_exists Function

Syntax

L Result = VARIABLE_EXISTS(variableName as c)

Arguments

variableNameCharacter

A string or string expression containing the name of a variable

Returns

ResultLogical

Returns .T. if variable exists, and .F. if it does not exist

Description

Return true if a variable exists.

Discussion

The eval_valid function used to test if a variable exists in Xbasic code, but was an expensive function to execute.

This is because it tested if an entire expression was valid,for example:

eval_valid("firstname + lastname")

In situations where there are a large number of calls to eval_valid(), the time taken by eval_valid() would become noticeable. As a result, the variable_exists() function was created specifically to test if a variable exists.

When the 'expression' tested by eval_valid() is simple (e.g. "Firstname"), then eval_valid() is functionally equivalent to variable_exists().

dim arr[1] as p
arr[1].foo.bar = "hello"
? variable_exists("arr[1].foo.bar")
= .T.
 
indx = 1
? variable_exists("arr["+indx+"].foo.bar")
= .T.
 
? variable_exists("arr[indx].foo.bar")
= .T. 
 
indx = 2
? variable_exists("arr["+indx+"].foo.bar")
= .F.
 
? variable_exists("arr[indx].foo.bar")
= .F.
 
'Forms can't be tested directly with this function, 
' but there are two alternatives
 
'This form exists in AlphaSports
p = form.load("customer information")
 
? Customer_Information.window_title
= "Customer.dbf : Form View (Customer Information)"
 
'Variable_exists() cannot be used on certain Alpha Five desktop objects, 
' such as forms
 
? variable_exists("Customer_Information.window_title")
= .F.
 
'Note that eval_valid() for the same argument is .t.
? eval_valid("Customer_Information.window_title")
= .T.
 
'On the other hand if we test p.window_title (since p is an ordinary xbasic variable),
' variable_exists() can be used
? variable_exists("p.window_title")
= .T.
variable_exists() is much faster than eval_valid() when testing Session variables and .NET variables, but only slightly faster than eval_valid() when testing ordinary Xbasic variables.

See Also