Declaring a Variable


Before a variable is used either by a script or in an expression, it must be declared. A variable can be declared either explicitly (as a formal declaration in a script, a table, a set, a form, or an application) or implicitly (by assigning it a value for the first time in a script). In general, a variable declaration must specify two things: the name of the variable, and the type of data the variable will contain.

If you place the OPTION STRICT command at the top of a script then you must declare variables explicitly using the DIM command.

Legal Variable Names

A variable name must start with a letter (from A to Z, a to z). Subsequent characters can be alphanumeric or an underscore (A to Z, a to z, 0 to 9, _). Variable names are not case-sensitive, so the names "GONZO" and "Gonzo" refer to the same thing. Variable names cannot contain space characters. Some variable names cannot be used because they conflict with the names of Xbasic functions, methods, or command statements. These pre-defined names are called reserved words. You cannot use a reserved word as a variable name. Variables names that include a period (.) are called dot variables. Dot variables are used extensively by certain Xbasic methods such as <TBL>.QUERY_CREATE().

Implicit Declaration

A variable is implicitly defined by assigning it a value with the assignment operator (" = "), using it in a for loop statement, or declaring it as a parameter of a user-defined function. The general formats are:

VariableName = expression
VariableName[Subscript] = expression
for VariableName = Start Value TO End Value
FUNCTION FunctionName as DataType (Parameter as Datatype, Parameter as DataType, ...)

Explicit Declaration

It is often important to explicitly define a variable before it is used. This is done with the DIM statement. Refer to the section describing the DIM statement for details on the syntax. Explicit declaration is necessary if the operation in which the variable is to be used is ambiguous. The DIM statement is also necessary if the variable is an array or is to be defined as a global or shared variable. A variable can also be explicitly declared in a table or set definition, or as Part of a layout.

Automatic Typecasting

When you append Variable2 to Variable1, Alpha Anywhere will automatically change the type of one of the variables to match the type of the other. If one of the variables is a character variable, typically that will be the resulting type. For example:

Value and Type
1 + "one"

"1one" Type: C

"one" + 1

"one1" Type: C

"" + 1

"1" Type: C

"today is " + date()

"today is 09/15/2004" Type: C

date()+ " is today"

"09/15/2004 is today" Type: C

"" + .T.

"True" Type: C

.F. + ""

"False" Type: C

date()+ 1

{09/16/2004} Type: D

Use the AUTOEXEC Script to Define Variables

If you want to have variables available from the start of your application, and available regardless of which forms have been opened, define them in an AUTOEXEC script.

See Also