Difference between revisions of "Record"

From Lazarus wiki
Jump to navigationJump to search
(link to tao yue's tutorial)
(rev)
Line 1: Line 1:
 
{{Record}}
 
{{Record}}
  
A '''record''' is a highly structured data [[Type|type]] in [[Pascal]] .  
+
A '''record''' is a highly structured data [[Type|type]] in [[Pascal]].
 +
They are widely used in Pascal, to group data items together logically.
  
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.
+
While simple data structures such as [[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.
  
 +
== Declaration ==
 
Some examples of records:
 
Some examples of records:
 
 
<syntaxhighlight>
 
<syntaxhighlight>
Type
+
type
  ExampleRecord = Record
+
TExampleRecord = record
                    Values: array [1..200] of real;
+
values: array [1..200] of real;
                    NumValues: Integer; { holds the actual number of points in the array }
+
numValues: integer; { holds the actual number of points in the array }
                    Average: Real { holds the average or mean of the values in the array }
+
avg: real { holds the average or mean of the values in the array }
                  End;
+
end;
 
+
  Member = Record
+
TMember = record
              Firstname, Surname : string;
+
firstname, surname : string;
              Address: array [1..3] of string;
+
address: array [1..3] of string;
              Phone : Integer;
+
phone: string;
              Birthdate: TDateTime;
+
birthdate: TDateTime;
              PaidCurrentSubscription: Boolean
+
paidCurrentSubscription: boolean
            End;
+
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
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:
+
== Addressing ==
 
+
=== fields ==
 +
Individual fields are accessed by placing a dot between the record name and the field name thus:
 
<syntaxhighlight>
 
<syntaxhighlight>
Var a, b : Member;
+
a.firstname := 'George';
Begin
+
a.surname := 'Petersen';
  { assign values to the fields in Record a }
+
a.phone := '789534';
  ...
+
a.paidCurrentSubscription := true;
  ...
 
  b := a
 
End;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Individual fields are accessed by placing a dot between the record name and the field name thus:
+
Alternatively, the whole series of fields can be made available together using the [[with]]-construct:
 
<syntaxhighlight>
 
<syntaxhighlight>
  a.firstname := 'George';
+
with a do
  a.surname := 'Petersen';
+
begin
  a.phone := 789534;
+
firstname := 'George';
  a.PaidCurrentSubscription := TRUE;
+
surname := 'Petersen';
 +
phone := '789534';
 +
paidCurrentSubscription := true
 +
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Alternatively, the whole series of fields can be made available together using the [[With|WITH]] construct:
+
=== 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:
 
<syntaxhighlight>
 
<syntaxhighlight>
with a  
+
var
  do
+
a, b: TMember;
  begin
+
 
    firstname := 'George';
+
(* main program *)
    surname := 'Petersen';
+
begin
    phone := 789534;
+
{ ... assign values to the fields in record a .. }
    PaidCurrentSubscription := TRUE
+
b := a
  end;
+
{ now b holds a copy of a }
 +
{ don't get confused with references: }
 +
{ a and b still point to different entities of TMember }
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Records are widely used in Pascal, to group data items together logically.
 
  
 
== See also ==
 
== See also ==
* [[Records]] Tutorial that covers records
+
* [[Records]], tutorial that covers records
  
 
{{Data types}}
 
{{Data types}}

Revision as of 19:56, 20 July 2014

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.

Declaration

Some examples of records:

type
	TExampleRecord = record
		values: array [1..200] of real;
		numValues: integer; { holds the actual number of points in the array }
		avg: real { holds the average or mean of the values in the array }
	end;
	
	TMember = record
		firstname, surname : string;
		address: array [1..3] of string;
		phone: string;
		birthdate: TDateTime;
		paidCurrentSubscription: boolean
	end;

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


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