Difference between revisions of "Record"
Line 6: | Line 6: | ||
While simple data structures such as [[Array|array]]s 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. | While simple data structures such as [[Array|array]]s 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. | Each separate part of a record is referred to as a field. | ||
+ | |||
+ | The record is [[Reserved_word|reserved word]]. | ||
== Declaration == | == Declaration == | ||
Line 55: | Line 57: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Alternatively, the whole series of fields can be made available together using the [[ | + | Alternatively, the whole series of fields can be made available together using the [[With]]-construct: |
<syntaxhighlight> | <syntaxhighlight> | ||
with a do | with a do |
Revision as of 17:19, 9 November 2015
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
magyar (hu) │
polski (pl) │
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 ist defined multiple times.
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 }
{ don't get confused with references: }
{ a and b still point to different entities of TMember }
end.
See also
- Records, tutorial that covers records
simple data types |
|
---|---|
complex data types |