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

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; for example, you can have a Handler part of type JSFHandler, otherwise known as a JSF handler). 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. 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]];

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

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;

Feedback