Declaring Variables
A variable is an identifier that references some value that can be changed. A variable declaration is the point in your program where you declare a new variable and designate its type.
Simple Declarations
Simple variables are declared using the following syntax:
{type} {identifier}
Where:
- {type} is one of the DataFlex simple data types (Integer, Date, String, Boolean, Variant, RowId, etc.) or a struct type.
- {identifier} is the name that you have assigned to the new variable. Note that you cannot use a DataFlex reserved word as a variable name.
A DataFlex variable declaration can occur anywhere within a program. However, it is good programming practice to declare all global variables near the top of your program and all local variables near the top of their respective Procedure or Function declarations.
Examples of Variable Declarations
Integer iNew // declares a new Integer variable called iNew
Number nNew // declares a new Number variable called nNew
Real rNew // declares a new Real variable called rNew
String sNew // declares a new String variable called sNew
Date dNew // declares a new Date variable called dNew
Variant vNew // declares a new Variant called vNew
RowId riCustomer // declares a new RowId variable called riCustomer
tContact myContact // declares a new tContact struct type variable called myContact
// (assuming the tContact struct type has been declared)
Multiple variables of the same type may be named on a single command line using the following syntax:
{type} {identifier1} {identifier2 … identifierN}
Examples of Declaring Multiple Variables
Integer iNew iOld
String sHot sMedium sCold
Each declared variable is initialized to its blank state (i.e., Integers, Numbers, and Reals are initialized to 0 (zero)); Strings and dates are initialized to "" (the empty string), and Variants are initialized to OLE_VT_EMPTY. Your program cannot use any variable until the line of code that contains its declaration is executed.
Array Declarations
Array variables are declared using the following syntax:
{base-type} {dimension-list} {identifier} […{identifier}]
Where:
- {base-type} is the type of data stored in each array element. This can be any one of the DataFlex simple types (Integer, Number, Real, Date, etc.) or a struct type.
- {dimension-list} specifies the number of dimensions and the maximum size of each dimension. Each dimension is specified by a pair of square brackets, i.e., []. The maximum size of each dimension is specified by an integer value between the brackets, e.g., [10].
Dynamic arrays are declared by omitting to specify the size of the first dimension, e.g., Integer[][4] iNames.
Jagged arrays are declared by omitting to specify the size of all dimensions, e.g., Integer[][] iNames.
- {identifier} is the name of the new array variable. Note that you cannot use a DataFlex reserved word as a variable name.
Examples of Declaring Arrays
Integer[][] iChessboard // Declares a 2-dimensional integer array called iChessboard
String[] sNames // Declares a single-dimensional array called sNames
Struct tContact
String Name
String Street
String City
String ZipCode
String Phone
End_Struct
tContact[] Contacts // Declares a single-dimensional array of type tContact
As with simple types, the elements in a declared static array are initialized to their blank state. For example, integer array elements are initialized to 0, strings to "", etc.
For more information, refer to Array Types.