Global Statement
Declares variables with the global scope. Global variables can be accessed from anywhere.
Syntax
Global [ Preserve ] dataType varName [(subscripts)] [, varName [(subscripts)] , ...]
Parameters
- Preserve
- If Preserve is specified, then the variable retains its values. The values are cleared by project changes. If Preserve is omitted, the variable doesn’t retain its values.
- dataType
- Specify the data type including Boolean, Byte, Double, Int32, Integer, Long, Real, Short, String, UByte, UInt32, or UShort.
- varName
- Specify a variable name of up to 32 characters.
- Maximum element number of the array variable
- Optional. Dimensions of an array variable; up to 3 dimensions may be declared. The subscripts syntax is as follows
(ubound1, [ubound2], [ubound3])
ubound1, ubound2, ubound3 each specify the maximum upper bound for the associated dimension. The elements in each dimension of an array are numbered from 0 and the available number of array elements is the upper bound value + 1.
The total available number of array elements for global variables is 10000 for strings and 100000 for all other types.
The total available number of array elements for global preserve variables is 400 for strings and 4000 for all other types.
To calculate the total elements used in an array, use the following formula. (If a dimension is not used, substitute 0 for the ubound value.) total elements = (ubound1 + 1) * (ubound2 + 1) * (ubound3 + 1)
Description
Global variables are variables which can be used in more than 1 file within the same project. They are cleared whenever a function is started from the Run window or Operator window unless they are declared with the Preserve option.
When declared in Preserve option, the variable retains the value at turning off the controller.
Global Preserve variables can be used with the RC+ Connectivity option.
It is recommended that global variable names begin with a "g_" prefix to make it easy to recognize globals in a program. For example:
Global Long g_PartsCount
See Also
Boolean, Byte, Double, Int32, Int64, Integer, Long, Real, Short, String, UByte, UInt32, UInt64, UShort
Global Statement Example
The following example shows 2 separate program files. The first program file defines some global variables and initializes them. The second file then also uses these global variables.
FILE1 (MAIN.PRG)
Global Integer g_Status
Global Real g_MaxValue
Function Main
g_Status = 10
g_MaxValue = 1.1
.
.
Fend
FILE2 (TEST.PRG) Function Test
Print "status1 =" , g_Status
Print "MaxValue =" , g_MaxValue
.
.
Fend