Home  |  Storing groups of data together

 Writing your first program
 Writing your second program
 Amending this program

 Delphi data types
   Text (strings and chars)
   Sets and enumerations

 Programming logic
   Exception handling

 Dates and times



 Printing text and graphics

 Object Orientation basics
   Memory leaks!
   An example class


 Standard components


 A brief history of Delphi

 Usability : file handling

 Usability : reference books

 Author links

  Storing groups of data together
What are records?
Records are a useful and distinguishing feature of delphi. They provide a very neat way of having named data structures - groups of data fields. Unlike arrays, a record may contain different types of data.
Records are fixed in size - the definition of a record must contain fixed length fields. We are allowed to have strings, but either their length must be specified (for example a : String[20]), or a pointer to the string is stored in the record. In this case, the record cannot be used to write the string to a file. The TPoint type is an example of a record. Before we go any further, let us look at a simple example.
   TCustomer = record
     name : string[30];
     age  : byte;
   customer : TCustomer;
  // Set up our customer record := 'Fred Bloggs';
   customer.age  := 23;

When we define a record, each field is simply accessed by name, separated by a dot from the record variable name. This makes records almost self documenting, and certainly easy to understand.
Above, we have created one customer, and set up the customer record fields.

Using the with keyword
When we are dealing with large records, we can avoid the need to type the record variable name. This avoidance, however, is at a price - it can make the code more difficult to read:
   TCustomer = record
     name : string[30];
     age  : byte;
   John, Nancy : TCustomer;
  // Set up our customer records
   with John do
     name := 'John Moffatt';              // Only refer to the record fields
     age  := 67;
   with Nancy do
     name := 'Nancy Moffatt';             // Only refer to the record fields
     age  := 77;

A more complex example
In practice, records are often more complex. Additionally, we may also have a lot of them, and might store them in an array. The following example is a complete program that you may copy and paste into your Delphi product, making sure to follow the instructions at the start of the code.
Please note that this is quite a complex piece of code - it uses a procedure that takes a variable number of parameters, specially passed in square brackets (see Procedure for more on procedures).
 // Full Unit code.
 // -----------------------------------------------------------
 // You must store this code in a unit called Unit1 with a form
 // called Form1 that has an OnCreate event called FormCreate.
 unit Unit1;
   Forms, Dialogs;
   TForm1 = class(TForm)
     procedure FormCreate(Sender: TObject);
     procedure ShowCustomer(const fields: array of string);
   Form1: TForm1;
 {$R *.dfm}// Include form definitions
 procedure TForm1.FormCreate(Sender: TObject);
  // Declare a customer record
   TCustomer = Record
     firstName : string[20];
     lastName  : string[20];
     address1  : string[100];
     address2  : string[100];
     address3  : string[100];
     city      : string[20];
     postCode  : string[8];
   customers : array[1..3] of TCustomer;
   i : Integer;
  // Set up the first customer record
   with customers[1] do
     firstName := 'John';
     lastName  := 'Smith';
     address1  := '7 Park Drive';
     address2  := 'Branston';
     address3  := 'Grimworth';
     city      := 'Banmore';
     postCode  := 'BNM 1AB';
  // Set up the second and third by copying from the first
   customers[2] := customers[1];
   customers[3] := customers[1];
  // And then changing the first name to suit in each case
   customers[2].firstName := 'Sarah';
   customers[3].firstName := 'Henry';
  // Now show the details of these customers
   for i := 1 to 3 do
     with customers[i] do ShowCustomer([firstName,
 // A procedure that displays a variable number of strings
 procedure TForm1.ShowCustomer(const fields: array of string);
   i : Integer;
  // Display all fields passed - note : arrays start at 0
   for i := 0 to Length(fields)-1 do

 The ShowMessage procedure is used to display the customer details.
 Click on it in the code to learn more.
 The displayed data is as follows:
 7 Park Drive
 7 Park Drive
 7 Park Drive

Packing record data
By default, Delphi will pad out the record with fillers, where necessary, to make sure that fields are aligned on 2, 4 or 8 byte boundaries to improve performance. You can pack the data with the packed keyword to reduce the record size if this is more important than performance. See Packed for more on this topic.

Records with variant parts
Things get very interesting now. There are times when a fixed format record is not useful. First, we may wish to store data in the record in different ways. Second, we may want to store different types of data in a part of a record.
The Delphi TRect type illustrates the first concept. It is defined like this:
   TRect = packed record
     case Integer of
       0: (Left, Top, Right, Bottom: Integer);
       1: (TopLeft, BottomRight: TPoint);

Here we have a record that holds the 4 coordinates of a rectangle. The Case clause tells Delphi to map the two following sub-sections onto the same area (the end) of the record. These variant sections must always be at the end of a record. Note also that the case statement has no end statement. This is omitted because the record finishes at the same point anyway.
The record allows us to store data in two ways:
   rect1, rect2 : TRect;
  // Setting up using integer coordinates
   rect1.Left   := 11;
   rect1.Top    := 22;
   rect1.Right  := 33;
   rect1.Bottom := 44;
  // Seting up rect2 to have the same coordinates, but using points instead
   rect2.TopLeft     := Point(11,22);
   rect2.BottomRight := Point(33,44);

The TRect record showed two methods of reading from and writing to a record. The second concept is to have two or more record sub-sections that have different formats and lengths.
This time we will define a fruit record that has a different attribute section depending on whether the fruit is round or long:
  // Declare a fruit record using case to choose the
  // diameter of a round fruit, or length and height ohterwise.
   TFruit = Record
     name : string[20];
     Case isRound : Boolean of// Choose how to map the next section
       True  :
         (diameter : Single); // Maps to same storage as length
       False :
         (length   : Single;  // Maps to same storage as diameter
          width    : Single);
   apple, banana : TFruit;
  // Set up the apple as round, with appropriate dimensions     := 'Apple';
   apple.isRound  := True;
   apple.diameter := 3.2;
  // Set up the banana as long, with appropriate dimensions    := 'Banana';
   banana.isRound := False;
   banana.length  := 7.65;
   banana.width   := 1.3;
  // Let us display the fruit dimensions:
   if apple.isRound
   then ShowMessageFmt('Apple diameter = %f',[apple.diameter])
   else ShowMessageFmt('Apple width = %f , length = %f',
                       [apple.width, apple.length]);
   if banana.isRound
   then ShowMessageFmt('Banana diameter = %f',[banana.diameter])
   else ShowMessageFmt('Banana width = %f , length = %f',
                       [banana.width, banana.length]);

 Apple diameter = 3.2
 Banana width = 3.20 , length = 7.65

Note that the Case statement now defines a variable, isRound to hold the type of the variant section. This is very useful, and recommended in variable length subsections, as seen in the code above.

Delphi Basics © Neil Moffatt All rights reserved.  |  Home Page