Data initialization
- Typically you can code an initializer (an equal sign
followed by a literal) in any of the following cases:
- When you define a structured Record part (for any lowest-level
field):
Record ExampleRecord type basicRecord 10 myField CHAR(5); 20 myField01 CHAR(1) = "1"; 20 myField02 CHAR(1) = "2"; 20 myArray01 CHAR(1)[3] = ["a", "b", "c"]; // the following entry assigns "z" to the first element // and (in Java code) blanks to the rest 20 myArray02 CHAR(1)[3] = ["z"]; endThis rule also applies to form fields. You cannot specify an initializer in a DataTable.
- When you declare a variable based on a primitive data type:
Record ExampleRecord type basicRecord myRecField INT = 2; end Program myProgram (myField03 INT = 3) myField04 STRING = "EGL"; function main() // myRecord.myRecField = 2 myRecord ExampleRecord; end end - When you define a record that redefines another record (the initializer
has no effect at declaration time, but is used if your code invokes
a set record initial statement):
Record partA 10 aa char(4) = "abcd"; end Record partB 10 bb char(4) = "1234"; end Program Example A partA; B partB { redefines="A" }; function main() // each of the next statements writes "abcd" writeStdOut( A.aa ); writeStdOut( B.bb ); // sets the memory area to reflect the definition of record partB set B initial; // each of the next statements writes "1234" writeStdOut( A.aa ); writeStdOut( B.bb ); end end
- When you define a structured Record part (for any lowest-level
field):
- EGL also initializes memory with preset values in the following
cases (those values are described later):
- Your logic invokes some variations of the set statement; see set.
- You set the initialized property of a variable to YES. This is a property of variables, not of record fields, and is largely obsolete.
- You generate a part for Java™, including libraries, services, and all handler types.
In a structured record, only the lowest-level structure fields are initialized. Consider the following example:
The memory area forRecord ExampleRecord 10 charField CHAR(24); 15 hexField HEX(16); 15 remainder CHAR(16); endhexFieldis initialized with binary zeros, as is appropriate for HEX variables. Theremainderfield is initialized with blanks. It takes two HEX characters to make a single byte, so a HEX(16) variable takes up only 8 bytes.In the case of an array, each member of the array is initialized individually.
Records or fields that are received as program or function arguments are never initialized automatically.
Nullable variables are a special case. Those created with the type extension character "?" are initialized to null.
Nullable variables are a special case. Those created with the type extension character "?" are initialized to null. Those governed by the i4glItemsNullable property have a different set of initial values. See i4glItemsNullable.
The following table details the automatic initialization values for various types:Table 1. Values automatically assigned during initialization Type Initialization value ANY Null BIN (and the integer types), HEX, FLOAT, SMALLFLOAT Binary zeros BLOB, CLOB Reset using lobLib.freeBlob() or lobLib.freeClob() BOOLEAN False CHAR, MBCHAR Single-byte blanks DATE, TIME, TIMESTAMP Value of the machine clock (for the number of bytes required by the mask, in the case of TIMESTAMP) DBCHAR Double-byte blanks DECIMAL, MONEY, NUM, NUMC, PACF Numeric zeros INTERVAL Numeric zeros (for the number of bytes required by the mask), preceded by a plus sign STRING "" (a null string) UNICODE Unicode blanks (each of which is hexadecimal 0020)
library lib1
a int = 1;
b int = lib2.c;
end
library lib2
c int = 2;
d int = lib1.a;
endIf a program attempts to use either lib1 or lib2, EGL throws an exception.
Compatibility
| Platform | Issue |
|---|---|
| COBOL generation | An EGL-generated COBOL program initializes all records. The fields within the records are initialized based upon the COBOL data type, after which the variables are initialized by EGL type as described in the table above, overriding the COBOL INITIALIZE statement. Note: If you generate a COBOL program that compares
a variable of type NUM with a variable of type CHAR, make sure that
your code initializes the fields; otherwise, the comparison can cause
the program to fail with an abend message. If this happens, no exception-handling
code is run. A similar, COBOL-specific warning applies to fields in
local records. |
| JavaScript™ generation | The following types are not supported: ArrayDictionary, BIN (with decimal places), BLOB, CHAR, CLOB, DBCHAR, HEX, INTERVAL, MBCHAR, NUMC, STRING (with a size limit), PACF, UNICODE, and structured Record parts. |