Difference between revisions of "Record"

From Lazarus wiki
m (Variable structure: 'Single' was used as identifier in a set! This is not recommended in Pascal. So I changed all identifiers in set 'maritalStates' to be prefixed with 'ms_'.)
(Undo revision 138498 by Crore (talk): keep it simple [and Pascal does not define “single”, so there’s no reason not to name an identifier like that])
(Tag: Undo)
Line 26: Line 26:
 
<syntaxhighlight lang="pascal" highlight="12-13">
 
<syntaxhighlight lang="pascal" highlight="12-13">
 
type
 
type
maritalStates = (ms_single, ms_married, ms_widowed, ms_divorced);
+
maritalStates = (single, married, widowed, divorced);
 
TPerson = record
 
TPerson = record
 
// CONSTANT PART
 
// CONSTANT PART
Line 38: Line 38:
 
// VARIABLE PART
 
// VARIABLE PART
 
case maritalStatus: maritalStates of
 
case maritalStatus: maritalStates of
ms_single: ( );
+
single: ( );
ms_married, ms_widowed: (marriageDate: TDateTime);
+
married, widowed: (marriageDate: TDateTime);
ms_divorced: (marriageDate, divorceDate: TDateTime;
+
divorced: (marriageDate, divorceDate: TDateTime;
 
isFirstDivorce: boolean)
 
isFirstDivorce: boolean)
 
end;
 
end;

Revision as of 19:50, 27 July 2020

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.

Declaration

Fixed structure

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

Variable structure

And even more complex structures are possible, eg:

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

The fields of the variable part are only available after assignment of its depending variable. It doesn't harm, whether the field marriageDate is defined multiple times.

Advanced record

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

Note-icon.png

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

Addressing

Fields

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

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

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

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

Instances

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:

var
	a, b: TMember;

(* main program *)
begin
	// ... 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.
end.


Constant record

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

const
	// 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