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 7 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

Entry

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

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


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

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

---

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, also 1 string parameter)

type TMyProcedureAsParameter = Procedure( s : string ) of Object;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PROCTestOne( s : string );
begin
 ShowMessage( 'PROCTestOne gets: ' + s );
end;

procedure TForm1.PROCTestTwo( s : string );
begin
 ShowMessage( 'PROCTestTwo gets: ' + s );
end;

procedure TForm1.PROCTestThree( s : string );
begin
 ShowMessage( 'PROCTestThree gets: ' + s );
end;

procedure TForm1.PROCTestFour( s : string );
begin
 ShowMessage( 'PROCTestFour gets: ' + s );
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) 1 parameter

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

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

end.

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

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

  +-------------------------------+
  |PROCTestOne gets: some string 1|
  +-------------------------------+

     clicking again on the button shows


  +-------------------------------+
  |PROCTestTwo gets: some string 2|
  +-------------------------------+

     clicking again on the button shows

  +---------------------------------+
  |PROCTestThree gets: some string 3|
  +---------------------------------+

     clicking again on the button shows

  +--------------------------------+
  |PROCTestFour gets: some string 4|
  +--------------------------------+

---
---

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

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