Pascal Programming/Records

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

Records[edit]

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:

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