Skip to content

Generics and templates

Here the main:

unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls,
  Employee,
  Genericpair;

type
  TPairStrInt = specialize TGenericpair<string,integer>;
  TPairStrStr = specialize TGenericpair<string,string>;
  TPairStrDouble = specialize TGenericpair<string,double>;
  TPairStrEmployee = specialize TGenericpair<string,TEmployee>;

type

  { TForm1 }

  TForm1 = class(TForm)
    ButtonGeneratePairs: TButton;
    Memo: TMemo;
    procedure ButtonGeneratePairsClick(Sender: TObject);
  private
    { private declarations }
    PairStrInt : TPairStrInt;
    PairStrStr : TPairStrStr;
    PairStrDouble : TPairStrDouble;
    PairStrEmployee : TPairStrEmployee;
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.ButtonGeneratePairsClick(Sender: TObject);
begin
    PairStrInt := TPairStrInt.Create('My string',1);
    PairStrStr := TPairStrStr.Create('My string one','My string two');
    PairStrDouble := TPairStrDouble.Create('My string',1.5);
    PairStrEmployee := TPairStrEmployee.Create('A Good employee is ...', TEmployee.Create('Demetrio','Stratos') );

    Memo.Clear;
    Memo.Lines.Add('First value : ' + PairStrInt.GetFirst + ', Second value : ' + inttostr(PairStrInt.GetSecond));
    Memo.Lines.Add('First value : ' + PairStrStr.GetFirst + ', Second value : ' + PairStrStr.GetSecond);
    Memo.Lines.Add('First value : ' + PairStrDouble.GetFirst + ', Second value : ' + floattostr(PairStrDouble.GetSecond));
    Memo.Lines.Add('First value : ' + PairStrEmployee.GetFirst + ', Second value : ' + TEmployee(PairStrEmployee.GetSecond).PrintMe );

    PairStrInt.Destroy;
    PairStrStr.Destroy;
    PairStrDouble.Destroy;
    TEmployee(PairStrEmployee.GetSecond).Destroy;
    PairStrEmployee.Destroy;
end;

initialization
  {$I unit1.lrs}

end.

 

Just one main event-driven procedure, on the button's click.