Declaring variables and constants

Variables and constants reserve storage in an EGL program. Constant values cannot change at run time.

You must explicitly declare all EGL variables. You can use variables without declaring them in some other languages, but not in EGL.

Syntax

Syntax diagram for variable declaration
varName
The name of the variable for which you are reserving storage.
type
Either a primitive type or a user-defined part on which the variable is based.
property
Optional property/value pairs for the variables.
initializer
You can specify an initial value for the variable.

Variables

You can declare a variable in these ways:
  • You can base a variable on one of the EGL primitive types (see "Primitive data types"), as in this example:
      customerBalance DECIMAL(9,2);
  • You can base a variable on a DataItem part or Record part, as in this example:
      myCustomer CustomerRecord;
  • You can base a variable on one of the predeclared parts, as in this example of a dictionary:
      myDictionary Dictionary 
      {
        empnum=0005,
        lastName="Twain",
        firstName="Mark",
        birthday="021460"
      };
  • You can specify properties (information about a part or variable) when you declare a variable based on a part. In some cases those properties override properties you declared when defining the part itself. For more information, see Properties.
  • You can declare a record that redefines the area of memory declared by another record. For more information, see redefines.
  • A program or other main part can access the fields of a DataTable, which is treated as a variable that is global to either the program or the run unit. You can use a simpler syntax to access those fields if the DataTable is listed in a use statement; see use.
  • A program can access the fields of a text or print form; the program treats the form as a variable that is global to the program. The program must include the related formGroup in a use statement; see use.
  • A program or other main logic part can access the library variables that are declared outside of any library function. Those variables are global to the run unit. You can use a simpler syntax to access those fields if you list the library in a use statement; see use.
  • A program can define whether the data associated with a specific record variable is saved and restored across a segmented converse in the segmented storage manager. The record variable annotation called enableSaveRestore controls whether the variable data is preserved or not. The result depends upon whether the value of YES or NO is specified. If the default value YES is used, the data associated with the record variable is preserved.

Type extension characters

The notion of a type describes the way a particular variable or expression represents a value. Types also determine compatibility; for instance, you can add the character representation of a number to a string, and you can add a number to the integer code point representation of a character, but you cannot add a number to a character.

EGL has primitive data types like INT and CHAR that are simple and straightforward. The language also has more complex parts that also function as types. Additionally, EGL uses a few special characters as type extensions when you define variables, as described in the following table:

Table 1. Type extension characters in EGL
Character(s) Example Meaning
  anInt INT; Single instance of this type; if this is a value type (not a reference) variable, it is a named area of storage containing the data.
[] anIntArray INT[]; An array of this type; see Arrays.
? aNullableInt INT?; A variable that can contain a null value; see Null values and the nullable type.

Assigning values to variables

If the variable is based on a primitive type, you can assign a value at the same time you declare it. Place the entire declaration on the left side of the assignment statement, as in the following example:
customerBalance DECIMAL(9,2) = 1001.22;

After you declare a variable, you can assign a new value at any time through regular assignment syntax. For more on this subject, see Assignments. For information about using literals in assignments, see Literals.

Constants

A constant is a declared value that cannot be changed at run time. Declare a constant with the reserved word const followed by the constant name, type, equal sign, and value. Alternatively, a dataitem can be used as the type and if that dataitem has a value= annotation specified, then its value will be used as the declared value, eliminating the need to have a equal sign and value on the const field. Only dataitem types of string, unicode, char or any of the non-floating point numeric values are supported with const and the value annotation. When specifying a numeric value, the value= annotation can be specified with or without quotes around the literal value. Examples are as follows:
  const copyrStr String = "Copyright 2007 by CompanyB";
  const myArray  BIN[] = [36, 49, 64];
  const myArray02 BIN[][] = [[1,2,3],[5,6,7]];
  const myValue myDataitem;
  
  dataitem myDataitem char(30) {value = "Manufactured by CompanyC"} end

You cannot include a constant in a record or other complex structure.

You can include the reserved word const as a modifier on function parameters. For more information, see Parameter modifiers.

To declare multiple variables or constants in a single statement, separate one identifier from the next by a comma, as in these examples:
  const myString01, myString02 STRING = "INITIAL";
  myVar01, myVar02, myVar03 CHAR(5);
  myRecord01, myRecord02 ExampleRecord;