Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: AnonymFirst.dproj

Project Structure

AnonymFirst.dpr
program AnonymFirst;

uses
  Forms,
  AnonymFirst_MainForm in 'AnonymFirst_MainForm.pas' {FormAnonymFirst};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFormAnonymFirst, FormAnonymFirst);
  Application.Run;
end.
AnonymFirst_MainForm.pas
unit AnonymFirst_MainForm;

interface

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


// anonymous method type declaration
type
  TIntProc = reference to procedure (n: Integer);

// method reference type declaration (not used, only to compare syntax)
type
  TIntMethod = procedure (n: Integer) of object;

type
  TFormAnonymFirst = class(TForm)
    btnSimpleVar: TButton;
    Memo1: TMemo;
    btnProcParam: TButton;
    btnLocalVal: TButton;
    btnStore: TButton;
    btnCall: TButton;
    btnNoParen: TButton;
    btReturn: TButton;
    procedure btnSimpleVarClick(Sender: TObject);
    procedure btnProcParamClick(Sender: TObject);
    procedure btnLocalValClick(Sender: TObject);
    procedure btnStoreClick(Sender: TObject);
    procedure btnCallClick(Sender: TObject);
    procedure btReturnClick(Sender: TObject);
  private
    FAnonMeth: TIntProc;
    procedure SetAnonMeth(const Value: TIntProc);
    function GetShowMethod: TIntProc;
  public
    property AnonMeth: TIntProc read FAnonMeth write SetAnonMeth;
  end;

var
  FormAnonymFirst: TFormAnonymFirst;

implementation

{$R *.dfm}

procedure CallTwice (value: Integer; anIntProc: TIntProc);
begin
  anIntProc (value);
  Inc (value);
  anIntProc (value);
end;

procedure TFormAnonymFirst.btnCallClick(Sender: TObject);
begin
  if Assigned (AnonMeth) then
  begin
    CallTwice (2, AnonMeth);
  end;
end;

procedure TFormAnonymFirst.btnLocalValClick(Sender: TObject);
var
  aNumber: Integer;
begin
  aNumber := 0;
  CallTwice (10,
    procedure (n: Integer)
    begin
      Inc (aNumber, n);
    end);
  Memo1.Lines.Add (IntToStr (aNumber));
end;

procedure TFormAnonymFirst.btnProcParamClick(Sender: TObject);
begin
  CallTwice (48,
    procedure (n: Integer)
    begin
      Memo1.Lines.Add (IntToHex (n, 4));
    end);
  CallTwice (100,
    procedure (n: Integer)
    begin
      Memo1.Lines.Add (FloatToStr(Sqrt(n)));
    end);
end;

procedure TFormAnonymFirst.btnSimpleVarClick(Sender: TObject);
var
  anIntProc: TIntProc;
begin
  anIntProc := procedure (n: Integer)
  begin
    Memo1.Lines.Add (IntToStr (n));
  end;
  anIntProc (22);
end;

procedure TFormAnonymFirst.btnStoreClick(Sender: TObject);
var
  aNumber: Integer;
begin
  aNumber := 3;
  AnonMeth :=
    procedure (n: Integer)
    begin
      Inc (aNumber, n);
      Memo1.Lines.Add (IntToStr (aNumber));
    end;
end;

function TFormAnonymFirst.GetShowMethod: TIntProc;
var
  x: Integer;
begin
  x := Random (100);
  Memo1.Lines.Add ('New x is ' + IntToStr (x));
  Result := procedure (n: Integer)
  begin
    x := x + n;
    Memo1.Lines.Add (IntToStr (x));
  end;
end;

procedure TFormAnonymFirst.btReturnClick(Sender: TObject);
var
  ip: TIntProc;
begin
  GetShowMethod; // compiles, but probably not what you want

  // call using a te
  // ip := GetShowMethod; // E2010 Incompatible types: 'TIntProc' and 'Procedure'
  ip := GetShowMethod();
  ip (3);

  // GetShowMethod(3); // E2034 Too many actual parameters
  GetShowMethod()(3);
  GetShowMethod.Invoke (3);
end;

procedure TFormAnonymFirst.SetAnonMeth(const Value: TIntProc);
begin
  FAnonMeth := Value;
end;

initialization
  Randomize;

end.
AnonymFirst_MainForm.pas.dfm
object FormAnonymFirst: TFormAnonymFirst
  Left = 0
  Top = 0
  Caption = 'AnonymFirst'
  ClientHeight = 310
  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 btnSimpleVar: TButton
    Left = 8
    Top = 8
    Width = 114
    Height = 25
    Caption = 'btnSimpleVar'
    TabOrder = 0
    OnClick = btnSimpleVarClick
  end
  object Memo1: TMemo
    Left = 136
    Top = 8
    Width = 409
    Height = 273
    TabOrder = 1
  end
  object btnProcParam: TButton
    Left = 8
    Top = 40
    Width = 114
    Height = 25
    Caption = 'btnProcParam'
    TabOrder = 2
    OnClick = btnProcParamClick
  end
  object btnLocalVal: TButton
    Left = 8
    Top = 72
    Width = 114
    Height = 25
    Caption = 'btnLocalVal'
    TabOrder = 3
    OnClick = btnLocalValClick
  end
  object btnStore: TButton
    Left = 8
    Top = 120
    Width = 114
    Height = 25
    Caption = 'btnStore'
    TabOrder = 4
    OnClick = btnStoreClick
  end
  object btnCall: TButton
    Left = 8
    Top = 152
    Width = 114
    Height = 25
    Caption = 'btnCall'
    TabOrder = 5
    OnClick = btnCallClick
  end
  object btnNoParen: TButton
    Left = 8
    Top = 184
    Width = 114
    Height = 25
    Caption = 'btnNoParen'
    TabOrder = 6
  end
  object btReturn: TButton
    Left = 8
    Top = 248
    Width = 114
    Height = 25
    Caption = 'btReturn'
    TabOrder = 7
    OnClick = btReturnClick
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù