You may wish to declare
a record that redefines an area of memory declared by another record. For
example, you can write a loop that reads one data area after another from
a serial file, when the structure of the retrieved data is different from
one retrieval to the next, as in the following example:
Record RecordA type SerialRecord
{ fileName = "myFile" }
record_type char(1);
item1 char(20);
end
Record RecordB type BasicRecord
10 record_type char(1);
10 item2 bigint;
10 item3 decimal(7);
10 item4 char(8);
end
Program ProgramX type basicProgram
myRecordA RecordA;
myRecordB RecordB {redefines = "myRecordA"};
function main();
get myRecordA;
while (myRecordA not endOfFile)
if (myRecordA.record_type == "A")
myFunction01(myRecordA.item1);
else
myFunction02(myRecordB.item2, myRecordB.item3, myRecordB.item4);
end
get myRecordA;
end
end
end
Within the loop, the function acts as follows:
- Checks the first field in the input record for a code that identifies
how the rest of the data is structured.
- Processes the other fields in the retrieved data by using either the input
record or a second, basic record. The second record refers to the same area
of memory as the input record but is structured differently.
To declare one record as a redefinition of another, you use the property redefines,
which accepts a string that identifies another record. This property is only
available in a record declaration, not in a record part definition.
The original and overlay record can be any types of fixed record, with
the following restrictions:
- The records must be in the same scope. If one record is declared as a
field of a non-fixed record, for example, the other record must be declared
as a field in the same non-fixed record. Similarly, if one record is declared
in a library but outside of a function, the other must be declared in the
same library but outside of a function.
- The overlay record must have the same length or be shorter than the original
record. This restriction prevents your code from accessing an area in the
overlay record that is not in the area of memory being redefined.
- Use of an SQL record (or any record that is nullable) must take into account
the hidden bytes that are described in SQL record internals.
An overlay record does not have any of the information (other
than structure) that is in the original record. An indexed record can redefine
a serial record, for example, but the file accessed by the indexed record
is identified in the indexed record part and not in the serial record part.