Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: ClassConstraint.dproj

Project Structure

ClassConstraint.dpr
program ClassConstraint;

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

{$R *.res}

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

interface

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

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

var
  Form30: TForm30;

implementation

{$R *.dfm}

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

{ TSampleClass<T> }

procedure TSampleClass<T>.One;
begin
  if Assigned (data) then
  begin
    Form30.Log('ClassName: ' + data.ClassName);
    Form30.Log('Size: ' + IntToStr (data.InstanceSize));
    Form30.Log('ToString: ' + data.ToString);
  end;
end;

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

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

{ TForm30 }

type
  TMyButton = class (TButton)
  public
    function ToString: string; override;
  end;

procedure TForm30.Button1Click(Sender: TObject);
var
  sample1: TSampleClass<TButton>;
  sample2: TSampleClass<TMyButton>;
  mb: TMybutton;
begin
  mb := TMyButton.Create(self);
  mb.Caption := 'hello';

  sample1 := TSampleClass<TButton>.Create;
  try
    sample1.SetT (mb);
    sample1.One;
  finally
    sample1.Free;
  end;

  sample2 := TSampleClass<TMyButton>.Create;
  try
    sample2.SetT (mb);
    sample2.One;
  finally
    sample2.Free;
  end;

end;

procedure TForm30.createClick(Sender: TObject);
var
  sample1: TSampleClass<TButton>;
  sample2: TSampleClass<TStrings>;
begin
  sample1 := TSampleClass<TButton>.Create;
  try
    sample1.SetT (Sender as TButton);
    sample1.One;
  finally
    sample1.Free;
  end;

  sample2 := TSampleClass<TStrings>.Create;
  try
    sample2.SetT (Memo1.Lines);
    sample2.One;
  finally
    sample2.Free;
  end;
end;

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

procedure TForm30.wrongClick(Sender: TObject);
//var
//  sample3: TSampleClass<Integer>;
//  // E2511 Type parameter 'T' must be a class type
begin
  // nothing do to
end;

{ TMyButton }

function TMyButton.ToString: string;
begin
  Result := Caption;
end;

end.
ClassConstraint_MainForm.pas.dfm
object Form30: TForm30
  Left = 0
  Top = 0
  Caption = 'ClassContraint'
  ClientHeight = 292
  ClientWidth = 554
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 104
    Top = 8
    Width = 433
    Height = 273
    TabOrder = 0
  end
  object create: TButton
    Left = 8
    Top = 8
    Width = 75
    Height = 25
    Caption = 'create'
    TabOrder = 1
    OnClick = createClick
  end
  object wrong: TButton
    Left = 8
    Top = 40
    Width = 75
    Height = 25
    Caption = 'wrong'
    TabOrder = 2
    OnClick = wrongClick
  end
  object Button1: TButton
    Left = 8
    Top = 72
    Width = 75
    Height = 25
    Caption = 'virtual'
    TabOrder = 3
    OnClick = Button1Click
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù