Entry
Delphi: Procedure: Function: Parameter: Pass: How to pass a procedure as a parameter: 0 parameters?
Feb 22nd, 2004 16:19
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 22 February 2004 - 09:19 pm -------------------
Delphi: Procedure: Function: Parameter: Pass: How to pass a procedure
as a parameter: 0 parameters?
---
---
Steps: Overview:
1. Create a new application
2. Put e.g. a button on the form
3. Add the following text
type TMyProcedureAsParameter = Procedure;
var myprocedureasparameter : TMyProcedureAsParameter;
4. -Create now one or more procedures without parameters
procedure PROCTestOne;
begin
ShowMessage( 'PROCTestOne' );
end;
procedure PROCTestTwo;
begin
ShowMessage( 'PROCTestTwo' );
end;
5. -Call then the corresponding procedure by passing
the name of that procedure as a parameter
1. first assigning the name to the 'var' variable
2. then calling that 'var' variable with some
parameter
myprocedureasparameter := PROCTestOne;
myprocedureasparameter;
myprocedureasparameter := PROCTestTwo;
myprocedureasparameter;
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);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure PROCTestOne;
begin
ShowMessage( 'PROCTestOne' );
end;
procedure PROCTestTwo;
begin
ShowMessage( 'PROCTestTwo' );
end;
procedure TForm1.Button1Click(Sender: TObject);
type TMyProcedureAsParameter = Procedure;
var myprocedureasparameter : TMyProcedureAsParameter;
begin
myprocedureasparameter := PROCTestOne;
myprocedureasparameter;
//
myprocedureasparameter := PROCTestTwo;
myprocedureasparameter;
end;
end.
--- cut here ---------------------------------------------------------
---
---
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
----------------------------------------------------------------------