Sign in Join
Wiki copytemporary

Record: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Record

OldNewLine
1+A `record` is a highly structured data [[Type|type]] in [[Pascal]]. They are widely used in Pascal, to group data items together logically.
2+
3+While simple data structures such as [[Array|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.
4+
5+The word `record` is a [[Reserved word|reserved word]].
6+
7+## Declaration
8+
9+### Fixed structure
10+
11+``` pascal
12+type
13+ TMember = record
14+ firstname, surname : string;
15+ address: array [1..3] of string;
16+ phone: string;
17+ birthdate: TDateTime;
18+ paidCurrentSubscription: boolean
19+ end;
20+```
21+
22+### Variable structure
23+
24+And even more complex structures are possible, e.g.:
25+
26+``` pascal
27+type
28+ TMaritalState = (unmarried, married, widowed, divorced);
29+ TPerson = record
30+ // CONSTANT PART
31+ // of course records may be nested
32+ name: record
33+ first, middle, last: string;
34+ end;
35+ sex: (male, female);
36+ // date of birth
37+ dob: TDateTime;
38+ // VARIABLE PART
39+ case maritalState: TMaritalState of
40+ unmarried: ( );
41+ married, widowed: (marriageDate: TDateTime);
42+ divorced: (marriageDateDivorced, divorceDate: TDateTime;
43+ isFirstDivorce: boolean)
44+ end;
45+```
46+
47+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*.
48+
49+The variable part shares the same memory. So `marriageDate` and `marriageDateDivorced` will have the same value regardless of `maritalState`. This behaviour is particularly practical as the following example shows:
50+
51+``` pascal
52+type
53+ TSpecialWord = record
54+ case Byte of
55+ 0: (Word_value: Word); // 7
56+ 1: (Byte_low, Byte_high: Byte); // 7, 0
57+ 2: (Bits: bitpacked array [0..15] of 0..1); // 1, 1, 1, 0, 0, ...
58+ end;
59+```
60+
61+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.
62+
63+### Advanced record
64+
65+An “advanced record” is a record with [[Method|methods]], [[Property|properties]] and [[management operators]]
66+
67+> **Note:** Remember to add a compiler directive `{$modeSwitch advancedRecords}` (after you have set the mode).
68+
69+## Addressing
70+
71+### Fields
72+
73+Individual fields are accessed by placing a dot between the record name and the field name thus:
74+
75+``` pascal
76+ a.firstname := 'George';
77+ a.surname := 'Petersen';
78+ a.phone := '789534';
79+ a.paidCurrentSubscription := true;
80+```
81+
82+Alternatively, the whole series of fields can be made available together using the [[With|with-construct]]:
83+
84+``` pascal
85+with a do
86+begin
87+ firstname := 'George';
88+ surname := 'Petersen';
89+ phone := '789534';
90+ paidCurrentSubscription := true
91+end;
92+```
93+
94+### Instances
95+
96+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:
97+
98+``` pascal
99+var
100+ a, b: TMember;
101+
102+(* main program *)
103+begin
104+ // ... assign values to the fields in record a
105+ b := a
106+ // Now b holds a _copy_ of a.
107+ // Do not get confused with references:
108+ // a and b still point to _different_ _entities_ of TMember.
109+end.
110+```
111+
112+## Constant record
113+
114+``` pascal
115+type
116+ // record definition
117+ TSpecialDay = record
118+ dayName: string;
119+ month: integer;
120+ day: integer;
121+ end;
122+
123+const
124+ // TSpecialDay constant
125+ christmasDay: TSpecialDay = (
126+ dayName: 'Christmas Day';
127+ month: 12;
128+ day: 25;
129+ );
130+
131+// since FPC 3.2.0 you may define an constant array of records like this
132+const
133+ SpecialDays: array of TSpecialDay = (
134+ ( dayName: 'Christmas Day' ; month: 12; day: 25),
135+ ( dayName: 'New Year''s Day'; month: 1; day: 1)
136+ );
137+```
138+
139+## Records compared to other structured types
140+
141+## See also
142+
143+- [[Basic Pascal Tutorial/Chapter 5/Records|Records]], tutorial that covers records
144+- [[Object|object]]
145+- [[Class|class]]
146+
147+---
148+*From the Free Pascal wiki, page "Record", as it stood on 2022-11-28 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
149+Category: Code

Back to history