References to variables and constants
Each data declaration (whether variable or constant) is a named area of memory that is either global to a program or local to a function--
- You can base a variable on one of several primitive types, as in this example:
myItem CHAR(10);For details, see Primitive types.
- You can base a variable on a dataItem part or record part, as in this example:
myRecord myRecordPart;For details, see Typedef.
- You can compose a program parameter as described previously for variables; alternatively, you can base it on a form part, as in this example:
myForm myFormPart;The form must be accessible through a form group that is identified in one of the program's use declarations. A form accessed as a parameter cannot be displayed to the user, but can provide access to field values that are passed from another program.
- Some parts are generated independent of the program and act as variables that are global to the program:
- If a part is of type dataTable, library, or pageHandler and is visible to the program, the part is treated as a program-global variable. For referencing convenience, you can include a dataTable or library part in one of the program's use declarations.
- If a part is of type formGroup, is visible to the program, and is included in one of the program's use declarations, the form parts that are referenced in that formGroup are also treated as program-global variables. All the fields in those forms are also available as program-global variables.
- Several rules concern the entities that are within parts that are available to the program:
- If a dataTable part is a program-global variable, the fields in that part are also global to the program.
- If a library part is a program-global variable, the following entities in that part are also global to the program:
- Public functions
- Public variables
- Public constants
For details on visibility, see References to parts. See also Use declaration.
- You can compose a function parameter on the basis of a primitive type, a dataItem part, or a record part; alternatively, you can base a function parameter on a loose primitive type, as in this example:
myNumber Number;In addition, a function parameter can be a dynamic array of records or data items. For details, see Function part in EGL source format.
- You declare a constant by associating an identifier with a number or quoted string. That initial value cannot be changed at run time, as in these examples:
myNumber 10.10; myString "Great software!";- Finally, to declare multiple variables or constants in a single statement, separate one identifier from the next by a comma, as in these examples:
myItem01, myItem02, myItem03 CHAR(5); myRecord01, myRecord02 myRecordPart; myNumber01, myNumber02 10.10;
Program variables and constants
When you declare a variable or constant in a program part, the memory is available throughout the program; in particular, the variable or constant is available in any function that the program invokes directly or indirectly.
Program variables are of two kinds:
- Program parameters are available only in a called program and refer to global memory that initially contains data received from another program. At run time, if the called program changes data that was passed by way of a variable, the storage area available to the caller is changed, too.
For a description of how data is provided when a main program is invoked, see Program part.
- Other program variables allocate program-global memory, as do program constants.
Each of the MQSeries options records that you specify in an MQ record property must be a program-global variable. For details, see Options records for MQ records.
A record identified in an I/O operation is called an I/O object.
Function variables
When you declare a variable in a function part, the variable is available only in the function. If Function01 declares Var01 and invokes Function02, for example, Function01 can reference the variable but Function02 cannot.
Function variables are of two kinds:
- Function parameters refer to memory that initially contains data received from an invoking function. At run time, if the invoked function changes data that was passed by way of a variable, the storage area available to the invoker is changed, too.
- Other function variables and function constants allocate local memory and refer to that memory.
For details, see Function invocations.
Arrays and items
In some contexts the logic part references a record as a whole, but in other contexts the logic part references an array or item:
- An array is a named, allocated area of memory that contains a series of values that have identical formats. For details on how to reference an array element, see Arrays.
- An item is a named area of memory that contains a single value. Such an area may be described by any of the following entities:
- A data item
- A structure item, so long as the structure item does not represent an array
- An array element, so long as the array element does not represent an array
An item (if not a data item) may have subordinate structure items, and any of those subordinates may represent an array.
Rules for referencing variables
The following rules are in effect:
- If you are referencing an item that is described by a structure item, you can avoid ambiguity about the area of memory being referenced. Consider the following part declaration, for example:
Record myRecordPart type serialRecord { fileName = myFile } 10 myTop; 20 myNext; 30 myAlmost; 40 myItem CHAR(10); 40 myItem02 CHAR(10); endAssume that a function or program uses the record part myRecordPart as a typeDef when declaring a variable named myRecordVar.
If you want to refer to an item that has the characteristics of a specific structure item, you can include the following symbols in order:
- The name of the variable that points to the format of the part; in this case, myRecordVar
- A period (.)
- A list of structure items superior to the item, with a period separating each structure item from the next; for example, myTop.myNext.myAlmost
- The item name preceded by a period; for example, .myItem
A valid reference to myItem in myRecordVar is as follows:
myRecordVar.myTop.myNext.myAlmost.myItemThat reference is considered to be fully qualified.
- If you want to refer to an item that has the characteristics of a structure item whose name is unique within a structure, you can specify the variable name, followed by a period, followed by the item name. Valid references for the earlier example include this symbol:
myRecordVar.myItem02That references are considered to be partially qualified.
You cannot partially qualify an item name in any other way. You cannot include only some of the structure item names that are between the variable name and the item name, for example, nor can you eliminate the variable name while keeping any of the names of structure items that are superior to the item. The following references are not valid for the earlier example:
// NOT valid myRecordVar.myNext.myItem myRecordVar.myAlmost.myItem myNext.myItem myAlmost.myItem- You can refer to an item without preceding the name with any qualifiers. Valid references for the earlier example include these symbols:
myItem02 myOtherItemThose references are considered to be unqualified.
- You must qualify any reference to a structure item to the extent necessary to avoid ambiguity. It is recommended that you use fully qualified names whenever possible. You must use an unqualified reference, however, when you are specifying a record property that refers to a key item, variable length item, or number of occurs item. (In an EGL statement, you can reference those special structure items as you would reference any structure item.)
- Unqualified and partially qualified references can be valid only if you set the property allowUnqualifiedItemReferences to yes. That property is a characteristic of programs, page handlers, and libraries, and the default value is no.
- The name of a structure item can be an asterisk (*) if the related memory area is a filler, which is an area whose name is of no importance. You cannot include an asterisk in a reference. Consider this example:
record myRecordPart type serialRecord { fileName = myFile } 10 person; 20 *; 30 streetAddress1 CHAR(30); 30 streetAddress2 CHAR(30); 30 nation CHAR(20); endIf you use that part as a typeDef when declaring the variable myRecordVar, you can refer to myRecordVar.nation or nation, but the following references are not valid:
// NOT valid myRecordVar.*.streetAddress1 myRecordVar.*.streetAddress2 myRecordVar.*.nation- When EGL tries to resolve a reference, names of local variables are searched first, then names of structure items in the records used for I/O in the same function, then names of other local structure items, then names that are program-global.
Consider the case in which a function declares both a data item called nation and a variable that points to the following basic record:
record myRecordPart 10 myTop; 20 myNext; 30 nation CHAR(20); endAn unqualified reference to nation refers to the data item, not to the structure item.
- A name search shows no preference for program-global data items over program-global structure items. Consider the case in which a program declares both a data item called nation and a variable that points to the format of the following basic record:
record myRecordPart 10 myTop; 20 myNext; 30 nation CHAR(20); endAn unqualified reference to nation fails because nation could refer either to the data item or to the structure item. You can reference the structure item, but only by qualifying the reference.
- The fully qualified name of a program variable must be unique among program variables; and the name of a function variable must be unique among function variables.
For additional rules, see Arrays and Use declaration.
This
Consider the following cases:
- The name of a function variable is identical to the name of a program-global data area in the program or page handler; or
- A library is referenced in your program's use declaration and has a data area whose name is the same as a program-global data area in the program or page handler.
To refer to the program-level data area in either of those cases, qualify the name of interest with the keyword this, as in the following example:
this.myProgramVariable
Related concepts
Function part
References to parts
Parts
Program part
Structure
Typedef
Related reference
Arrays
Function invocations
Function part in EGL source format
Options records for MQ records
Primitive types
Use declaration