Pascal Programming/Records

From Wikibooks, open books for an open world
Jump to navigation Jump to search


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 data as follows:

  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

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

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

The 'with' clause[edit]

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
  name := 'Bob';
  age := 37;
  gender := Male;