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:

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 are:

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 … indentifierN}

Examples of declaring multiple variables on a single command line are:

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:

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.

Examples of declaring arrays are:

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. e.g. integer array elements are initialized to 0, strings to "", etc.

For more information refer to Array Types.

See Also

Local Variables

Global Variables

Data Types, Variables and Constants