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

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:

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);
      end

    Assume 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.myItem

    That 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.myItem02

    That 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
      myOtherItem

    Those 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);
      end

    If 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);
      end

    An 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);
      end

    An 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