From Lazarus wiki
Revision as of 18:58, 29 July 2020 by Crore (talk | contribs) (Variable structure: Correcting old error with duplicate identifier and misleading comment about variable parts. Added new example.)

Deutsch (de) English (en) español (es) suomi (fi) français (fr) magyar (hu) português (pt) русский (ru)

A record is a highly structured data type in Pascal. They are widely used in Pascal, to group data items together logically.

While simple data structures such as arrays or sets consist of elements all of the same type, a record can consist of a number of elements of different types, and can take on a huge complexity. Each separate part of a record is referred to as a field.

The record is reserved word.


Fixed structure

	TMember = record
		firstname, surname : string;
		address: array [1..3] of string;
		phone: string;
		birthdate: TDateTime;
		paidCurrentSubscription: boolean

Variable structure

And even more complex structures are possible, e.g.:

	maritalStates = (single, married, widowed, divorced);
	TPerson = record
		// of course records may be nested
		name: record
			first, middle, last: string;
		sex: (male, female);
		// date of birth
		dob: TDateTime;
		case maritalStatus: maritalStates of
			single: ( );
			married, widowed: (marriageDate: TDateTime);
			divorced: (marriageDateDivorced, divorceDate: TDateTime;
				isFirstDivorce: boolean)			

Note that fields of the variable part have to be in parentheses. You cannot use the same identifier multiple times, so a slightly different name has to be used for the marriageDate in case of divorced.

The variable part shares the same memory. So marriageDate and marriageDateDivorced will have the same value regardless of maritalStatus. This behaviour is particularly practical as the following example shows:

  TSpecialWord = record
    case Byte of
      0: (Word_value: Word);                      // 7
      1: (Byte_low, Byte_high: Byte);             // 7, 0
      2: (Bits: bitpacked array [0..15] of 0..1); // 1, 1, 1, 0, 0, ...

This record has only a variable part and enables access to the value of the word, the individual bytes and even to the bits. An identifier is not necessarily required in the case clause, so this does not occupy any memory. The size of this record is two bytes. In the case of Bits, this is only possible if bitpacked is used. Note the order of the bytes, with the less significant byte (LSB) coming first.

Advanced record

An “advanced record” is a record with methods and properties.


Note: Remember to add a compiler directive {$modeSwitch advancedRecords} (after you have set the mode).



Individual fields are accessed by placing a dot between the record name and the field name thus:

	a.firstname := 'George';
	a.surname := 'Petersen'; := '789534';
	a.paidCurrentSubscription := true;

Alternatively, the whole series of fields can be made available together using the with-construct:

with a do
	firstname := 'George';
	surname := 'Petersen';
	phone := '789534';
	paidCurrentSubscription := true


A record is treated by the program as a single entity, and for example a whole record can be copied (provided the copy is of the same type) thus:

	a, b: TMember;

(* main program *)
	// ... assign values to the fields in record a
	b := a
	// Now b holds a _copy_ of a.
	// Do not get confused with references:
	// a and b still point to _different_ _entities_ of TMember.

Constant record

	// record definition
	TSpecialDay = record
		dayName: string;
		month: integer;
		day: integer;

	// TSpecialDay constant
	christmasDay: TSpecialDay = (
		dayName: 'Christmas Day';
		month: 12;
		day: 25;

See also

navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring