Declare a variable or array that is available to the entire application or module.
[Attributes] [Modifiers] GLOBAL <idVar> [:= <uValue>] [AS | IS <idType>]
[Attributes] [Modifiers] GLOBAL DIM <ArraySpec> AS | IS <idType>
Attributes | An optional list of one or more attributes that describe meta information for am entity, such as for example the [TestMethod] attribute on a method/function containing tests in a MsTest class library. Please note that Attributes must be on the same line or suffixed with a semi colon when they are written on the line above that keyword. |
Modifiers | An optional list of modifiers that specify the visibility or scope of the entity, such as PUBLIC, STATIC, INTERNAL, EXPORT and UNSAFE. |
<idVar> | A valid identifier name for the variable. A global variable is an entity and, as such, shares the same name space as other entities. This means that it is not possible to have a global variable and a function, for example, with the same name. |
<uValue> | A constant value that is assigned to <idVar>. This value can be a literal representation of one of the data types listed below or a simple expression involving only operators, literals, and DEFINE constants; however, more complicated expressions (including class instantiation) are not allowed. |
Note: Although <uValue> can be a literal array, it must be one-dimensional. Multi-dimensional literal arrays are not allowed. For example, {1, 2, 3} is allowed, but { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } is not. |
Note: Although the Chr() function cannot be used in <uValue>, the _Chr() operator can. _Chr() is otherwise identical in functionality to Chr(). |
If <uValue> is not specified, the initial value of the variable depends on the data type you declare (e.g., NIL if you do not use strong typing, 0 for AS INT, etc.) |
DIM <ArraySpec> | The specification for a dimensioned array to declare. |
<ArraySpec> | The specification for a dynamic array to declare. |
In both cases, <ArraySpec> is one of the following: |
<idArray>[<nElements>, <nElements>, <nElements>] |
<idArray>[<nElements>][<nElements>][<nElements>] |
All dimensions except the first are optional. |
<idArray> is a valid identifier name for the array to declare. For dynamic arrays, array elements are initialized to NIL. For dimensioned arrays, the initial value of the elements depends on the data type as explained above for <uValue>. |
<nElements> defines the number of elements in a particular dimension of an array. The number of dimensions is determined by how many <nElements> arguments you specify. |
<nElements> can be a literal numeric representation or a simple numeric expression involving only operators, literals, and DEFINE constants; however, more complicated expressions (such as function calls) are not allowed. |
AS <idType> | Specifies the data type. If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler. |
IS <idType> | Specifies a VOSTRUCT or UNION data type in which the memory needed to hold the structure is allocated on the stack (<idStructure> is the only <idType> allowed with the IS keyword.) See the VOSTRUCT entry in this guide for more information on data structure memory allocation. |
Search order for variables: You can hide a global variable name from a routine by declaring another variable with the same name (with LOCAL, MEMVAR, or FIELD). The search order for a variable name is as follows:
1. LOCALs, local parameters, MEMVARs, and FIELDs
2. SELF instance variables (i.e., without <idObject>: prefix in class methods)
3. GLOBALs and DEFINEs
The following example illustrates using the GLOBAL statement to create a global variable, a global dimensioned array, and a global dynamic array. The dynamic array, since it is declared with STATIC GLOBAL, is visible only in the current module:
GLOBAL cAppName := "Accounts Payable" AS STRING
GLOBAL DIM aiValues[2][10] AS INT
STATIC GLOBAL aPoly[100]
...
FUNCTION Start()
? "Start of ", cAppName, " application."
AFill(aPoly, 0)
...
? "End of ", cAppName, " application."