Difference between revisions of "Record"

From Lazarus wiki
Jump to navigationJump to search
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 7: Line 7:
 
Some examples of records:
 
Some examples of records:
  
<delphi>
+
<syntaxhighlight>
 
  Type
 
  Type
 
   ExampleRecord = Record
 
   ExampleRecord = Record
Line 22: Line 22:
 
               PaidCurrentSubscription: Boolean
 
               PaidCurrentSubscription: Boolean
 
             End;
 
             End;
</delphi>
+
</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:
 
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:
  
<delphi>
+
<syntaxhighlight>
 
  Var a, b : Member;
 
  Var a, b : Member;
 
  Begin
 
  Begin
Line 34: Line 34:
 
   b := a
 
   b := a
 
  End;
 
  End;
</delphi>
+
</syntaxhighlight>
  
 
Individual fields are accessed by placing a dot between the record name and the field name thus:
 
Individual fields are accessed by placing a dot between the record name and the field name thus:
<delphi>
+
<syntaxhighlight>
 
   a.firstname := 'George';
 
   a.firstname := 'George';
 
   a.surname := 'Petersen';
 
   a.surname := 'Petersen';
 
   a.phone := 789534;
 
   a.phone := 789534;
 
   a.PaidCurrentSubscription := TRUE;
 
   a.PaidCurrentSubscription := TRUE;
</delphi>
+
</syntaxhighlight>
  
 
Alternatively, the whole series of fields can be made available together using the [[With|WITH]] construct:
 
Alternatively, the whole series of fields can be made available together using the [[With|WITH]] construct:
<delphi>
+
<syntaxhighlight>
 
  with a  
 
  with a  
 
   do
 
   do
Line 54: Line 54:
 
     PaidCurrentSubscription := TRUE
 
     PaidCurrentSubscription := TRUE
 
   end;
 
   end;
</delphi>
+
</syntaxhighlight>
  
 
Records are widely used in Pascal, to group data items together logically.
 
Records are widely used in Pascal, to group data items together logically.

Revision as of 15:54, 24 March 2012

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

A highly structured data type in Pascal .

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.

Some examples of records:

 Type
   ExampleRecord = Record
                     Values: array [1..200] of real;
                     NumValues: Integer; { holds the actual number of points in the array }
                     Average: Real { holds the average or mean of the values in the array }
                   End;

   Member = Record
              Firstname, Surname : string;
              Address: array [1..3] of string;
              Phone : Integer;
              Birthdate: TDateTime;
              PaidCurrentSubscription: Boolean
            End;

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 : Member;
 Begin
   { assign values to the fields in Record a }
   ...
   ...
   b := a
 End;

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;

Records are widely used in Pascal, to group data items together logically.