Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: GenericTests.dproj

Project Structure

GenericTests.dpr
program GenericTests;

uses
  Forms,
  GenericTestsForm in 'GenericTestsForm.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
GenericTestsForm.pas
unit GenericTestsForm;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

type
  TMyClass <T> = class
    procedure GenericHello;
  end;

  TMyRecord <T> = record
    one: string;
    two: T;
  end;

// procedure GenProc <T> (value: T);
// E2530 Type parameters not allowed on global procedure or function

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  obj: TMyClass<string>;
begin
  obj := TMyClass<string>.Create;
  obj.GenericHello;
  obj.Free;
end;

{ TMyClass<T> }

procedure TMyClass<T>.GenericHello;
begin
  ShowMessage ('Hello');
end;

end.
GenericTestsForm.pas.dfm
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 276
  ClientWidth = 417
  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 = 112
    Top = 128
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù