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?

Entry

Delphi: Procedure: Function: Dynamic: Simple: How pass procedure method as parameter: 1 parameter?

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


----------------------------------------------------------------------
--- Knud van Eeden --- 23 February 2004 - 11:48 pm -------------------

Delphi: Procedure: Function: Dynamic: Simple: How pass procedure 
method as parameter: 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

     // this does the trick

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

     var myprocedureasparameter : TMyProcedureAsParameter;

 4. -Create now one or more procedures which have also one parameter of
     the same type as declared above (e.g. a string).

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

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

     and declare this procedure also in the interface

      procedure PROCTestOne( s : string );

      procedure PROCTestTwo( s : string );

 5. -Call then the corresponding procedure by passing
     the name of that procedure as a parameter by

     1. first assigning the name to the 'var' variable

     2. then calling that 'var' variable with some
        parameter

         myprocedureasparameter := PROCTestOne;
         myprocedureasparameter( 'some string 1' );

         myprocedureasparameter := PROCTestTwo;
         myprocedureasparameter( 'some string 2' );

 6. -So all together this becomes:

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

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure PROCTestOne( s : string );
    procedure PROCTestTwo( s : 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.Button1Click(Sender: TObject);
 type TMyProcedureAsParameter = Procedure( s : string );
 var myprocedureasparameter : TMyProcedureAsParameter;
begin
 myprocedureasparameter := PROCTestOne;
 myprocedureasparameter( 'some string 1' );
 //
 myprocedureasparameter := PROCTestTwo;
 myprocedureasparameter( 'some string 2' );
end;

end.

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

 7. -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|
  +-------------------------------+

---
---

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

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