Records

A record (analogous to a structure in some languages) represents a heterogeneous set of elements. Each element is called a field; the declaration of a record type specifies a name and type for each field. The syntax of a record type declaration is type recordTypeName = record fieldListl: typel;

fieldListn: typen; end where recordTypeName is a valid identifier, each type denotes a type, and each fieldList is a valid identifier or a comma-delimited list of identifiers. The final semicolon is optional.

For example, the following declaration creates a record type called TDateRec. type

TDateRec = record Year: Integer;

Each TDateRec contains three fields: an integer value called Year, a value of an enumerated type called Month, and another integer between 1 and 31 called Day. The identifiers Year, Month, and Day are the field designators for TDateRec, and they behave like variables. The TDateRec type declaration, however, does not allocate any memory for the Year, Month, and Day fields; memory is allocated when you instantiate the record, like this:

var Recordl, Record2: TDateRec;

This variable declaration creates two instances of TDateRec, called Recordl and Record2. You can access the fields of a record by qualifying the field designators with the record's name:

Recordl.Year := l904; Recordl.Month := Jun; Recordl.Day := l6;

Or use a with statement:

with Recordl do begin

You can now copy the values of Record1's fields to Record2: Record2 := Record1;

Because the scope of a field designator is limited to the record in which it occurs, you don't have to worry about naming conflicts between field designators and other variables.

Instead of defining record types, you can use the record ... construction directly in variable declarations:

var s: record

Name: string; Age: Integer; end;

However, a declaration like this largely defeats the purpose of records, which is to avoid repetitive coding of similar groups of variables. Moreover, separately declared records of this kind will not be assignment-compatible, even if their structures are identical.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment