Properties

When visual programming using forms in Delphi or Lazarus you will frequently write code such as lblResult.caption := 'Draw', where you appear to be assigning the string 'Draw' to the variable caption. However, caption is a property. Properties allow the programmer to attach additional processing to what appear to be simple assignments. Behind the scenes in this case, your apparent assignment is calling a SetText method (which may in turn call other methods). The SetText method could, for example, do nothing if the current value is 'Draw'.

In program PropertyDemo, the data is stored in private fields fName and fPoints. The following declarations are crucial.
property Name : string read GetName write SetName;
property Points : integer read GetPoints write SetPoints;  
 

As a consequence, the statement Player1.Name := Forename calls the method SetName and the statement writeln(Player1.Name, ' wins.') calls the method GetName. The statement Player1.Points := Player1.Points + 1 is more complicated. It calls GetPoints then adds 1 to the value returned to obtain the argument for the call to SetPoints.

The SetName method contains validation to ensure that fName contains only letters or a hyphen and SetPoints does not allow fPoints to be negative.

program PropertyDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils, GamePlayerProps in 'GamePlayerProps.pas';
var
  Player1, Player2 : TPlayer;
  Random1, Random2, i : integer;
  Forename : string;

begin
  Player1 := TPlayer.Create;
  repeat
    write('Please enter your forename. ');
    readln(Forename);
    Player1.Name := Forename;
  until Player1.Name <> '';
  Player2 := TPlayer.Create;
  Player2.Name := 'PC';
  randomize;
  for i := 1 to 5 do
    begin
      Random1 := random(4);
      writeln(Player1.Name,' draws number ', Random1);
      repeat
        Random2 := random(4);
      until Random2 <> Random1;
      writeln(Player2.Name,' draws number ', Random2);
      writeln;
      if Random1 > Random2 then
        Player1.Points := Player1.Points + 1
      else
        Player2.Points := Player2.Points + 1;
    end;
    Player1.ShowPoints;
    Player2.ShowPoints;
    writeln;
    if Player1.Points > Player2.Points then
        writeln(Player1.Name, ' wins.')
    else
      writeln(Player2.Name, ' wins.');
  Player1.Free;
  Player2.Free;
  readln;
end.

unit GamePlayerProps;

interface

type
  TPlayer = class(TObject)
  private
     //private field fName will be accessed via public property Name
    fName : string;
    fPoints : integer;
  public
    procedure SetName(PlayerName : string);
    function GetName : string;
    procedure SetPoints(iPoints : integer);
    function GetPoints : integer;
    property Name : string read GetName write SetName;
    property Points : integer read GetPoints write SetPoints;
    procedure ShowPoints;
  end;
  
implementation

procedure TPlayer.ShowPoints;

begin
  write('Points for ', Name ,': ', Points);
  writeln;
end;

procedure TPlayer.SetName (PlayerName : string);
var
  i : integer;
  Error : Boolean;
begin
  Error := False;
  i := 0;
  repeat
    inc(i);
    if not (PlayerName[i] in ['A' .. 'Z', 'a' .. 'z', '-']) then
      Error := True;
    until (i = length(PlayerName)) or (Error = True);
    if Error then
      writeln('Only letters and hyphen allowed')
    else
      fName := PlayerName;
end;

function TPlayer.GetName  : string;
begin
  result := fName;
end;

procedure TPlayer.SetPoints(iPoints : integer);
begin
  if iPoints >=0 then
    fPoints := iPoints
  else
    writeln('Points cannot be negative');
end;

function TPlayer.GetPoints : integer;
begin
  result := fPoints;
end;

end. 

Experimenting with Properties

  1. Modify the code for unit Shapes used by program InheritanceDemo so that the fields are accessed by properties. Change the code in program InheritanceDemo so that these properties are modified by code such as Rect.IsFilled := true.
  2. Modify code in other classes that you have written to include some properties.
Programming - a skill for life!

Classes (including the inbuilt TQueue and TStringList), objects, inheritance and properties