Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: GenericCodeGen.dproj

Project Structure

GenericCodeGen.dpr
program GenericCodeGen;

uses
  Forms,
  GenericCodeGen_MainForm in 'GenericCodeGen_MainForm.pas' {Form30};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm30, Form30);
  Application.Run;
end.
GenericCodeGen_MainForm.pas
unit GenericCodeGen_MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm30 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Log (const strMsg: string);
  end;

var
  Form30: TForm30;

implementation

{$R *.dfm}

type
  TSampleClass <T> = class
  private
    data: T;
  public
    procedure One;
    function ReadT: T;
    procedure SetT (value: T);
  end;

procedure TForm30.Button1Click(Sender: TObject);
var
  t1: TSampleClass<Integer>;
  t2: TSampleClass<string>;
begin
  t1 := TSampleClass<Integer>.Create;
  t1.SetT (10);
  t1.One;

  t2 := TSampleClass<string>.Create;
  t2.SetT ('hello');
  t2.One;

  Log ('t1.SetT: ' + IntToHex (PInteger(@TSampleClass<Integer>.SetT)^, 8));
  Log ('t2.SetT: ' + IntToHex (PInteger(@TSampleClass<string>.SetT)^, 8));

  Log ('t1.One: ' + IntToHex (PInteger(@TSampleClass<Integer>.One)^, 8));
  Log ('t2.One: ' + IntToHex (PInteger(@TSampleClass<string>.One)^, 8));
end;

{ TSampleClass<T> }

procedure TSampleClass<T>.One;
begin
  Form30.Log ('OneT');
end;

function TSampleClass<T>.ReadT: T;
begin
  Result := data;
end;

procedure TSampleClass<T>.SetT(value: T);
begin
  data := value;
end;

procedure TForm30.Log(const strMsg: string);
begin
  Memo1.Lines.Add(strMsg);
 end;

end.
GenericCodeGen_MainForm.pas.dfm
object Form30: TForm30
  Left = 0
  Top = 0
  Caption = 'GenericCodeGen'
  ClientHeight = 315
  ClientWidth = 570
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 24
    Top = 24
    Width = 75
    Height = 25
    Caption = 'Show @'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 136
    Top = 24
    Width = 417
    Height = 273
    TabOrder = 1
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù