< Pascal Programming

Records

Often, it becomes necessary for programmers to want or need to store structured data, such as the attributes of a person, for example. In Pascal, we can declare a record type. This is a user-defined data type to store structured daten as follows:

type
  TPerson = record
    name: String[16];
    age: Integer;
    gender: TGender; // TGender is assumed here to be an enumerated type that holds the values Male and Female
  end;

Next, we can use TPerson as a type in our code in the following ways.

var
  p: TPerson;
  x: integer;
begin
  p.name := 'Bob';
  p.age := 37;
  p.gender := Male;
  x := p.age;
end;

The 'with' clause

As you can see in the above example, the calls to our record variable get a little tedious. Fortunately, Pascal offers us the 'with' clause. This will ease the way you use record (and Turbo Pascal-compatible object) variables.

with p do
begin
  name := 'Bob';
  age := 37;
  gender := Male;
end;
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.