faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 7 of 8 people (88%) answered Yes

Entry

Delphi: Parameter: How to pass procedure method as parameter to central procedure: 0 parameters?

Feb 23rd, 2004 19:42
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 24 February 2004 - 04:29 am -------------------

Delphi: Parameter: How to pass procedure method as parameter to 
central procedure: 0 parameters?

---

The trick is to use '= Procedure of Object' (instead of '= Procedure').

---
---

Steps: Overview:

 1. Create a new application

 2. Put e.g. a button on the form

 3. Add the following text all together:

--- cut here ---------------------------------------------------------

unit Unit1;

interface

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

// Step 1: add this on top, so that it is known in e.g. PROCTestCentral
//         here below (and in this example, 0 parameters)

type TMyProcedureAsParameter = Procedure of Object;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure PROCTestOne;
    procedure PROCTestTwo;
    procedure PROCTestThree;
    procedure PROCTestFour;
    //
    // Step 2: declare PROCTestCentral as usual here
    //
    procedure PROCTestCentral( myprocedureasparameter :
TMyProcedureAsParameter );
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PROCTestOne;
begin
 ShowMessage( 'PROCTestOne' );
end;

procedure TForm1.PROCTestTwo;
begin
 ShowMessage( 'PROCTestTwo' );
end;

procedure TForm1.PROCTestThree;
begin
 ShowMessage( 'PROCTestThree' );
end;

procedure TForm1.PROCTestFour;
begin
 ShowMessage( 'PROCTestFour' );
end;

// ... and so on, e.g. handy when creating a calculator with
// ... methods like 'add', 'multiply', ...

// Step 3: Add your central procedure with the procedure name as a
//         parameter and (in this example) 0 parameters

procedure TForm1.PROCTestCentral( myprocedureasparameter :
TMyProcedureAsParameter );
begin
 myprocedureasparameter;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 // Step 4: so here you pass both the procedure name
 //         and its (in this example) 0 parameter value
 PROCTestCentral( PROCTestOne );
 PROCTestCentral( PROCTestTwo );
 PROCTestCentral( PROCTestThree );
 PROCTestCentral( PROCTestFour );
end;

end.

--- cut here ---------------------------------------------------------

 4. -If you run this source code, click on the button,
     and you will see:

  +-----------+
  |PROCTestOne|
  +-----------+

     clicking again on the button shows


  +-----------+
  |PROCTestTwo|
  +-----------+

     clicking again on the button shows

  +-------------+
  |PROCTestThree|
  +-------------+

     clicking again on the button shows

  +------------+
  |PROCTestFour|
  +------------+

---
---

Internet: see also:

---

Delphi: Procedure: Function: Dynamic: Can you give overview passing 
function/procedure as parameter?
http://www.faqts.com/knowledge_base/view.phtml/aid/28931/fid/175

----------------------------------------------------------------------