Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2001, 09:27 AM
aqiruse aqiruse is offline
Junior Member
 
Join Date: Jan 2001
Posts: 7
Default Passing functions as parameters

How do we pass functions as parameters in delphi?

What I have tried is this:

TMyType = procedure(Sender : TComponent; Notify : TMyEnumType);

procedure AddItem(item : TMyType)
begin
...
item(self, rOpen);
....
end;

Then in a seperate class I define:
procedure MyProc(Sender : TComponent; Notify : TMyEnumType);

procedure MyProc(Sender : TComponent; Notify : TMyEnumType);
begin
...
...
...
end;

In another function I try to send it to the 1st class from where i will call it...

procedure Add();
begin
FirstClass.AddItem(@MyProc);
end;

It keeps telling me that a variable is required, when I put in two dummy variables, it still tells me variable required...
Reply With Quote
  #2  
Old 01-11-2001, 10:25 AM
The Smurf The Smurf is offline
Member
 
Join Date: Jan 2001
Posts: 40
Default RE: Passing functions as parameters

Sorry to disappoint, but you can't directly pass a function name as a variable to another function.

I'm assuming you want to do this because you have several functions and you only want to call one, depending on the circumstances. So, create a DLL that exports all of the functions that you need. Then call them like this:

procedure AddItem(functionname: String);
type
TMyType = procedure(Sender: TComponent; Notify: TMyEnumType);
var
MyType: TMyType;
DLLInstance: THandle;
begin
DLLInstance:= LoadLibrary('dll.dll');
@MyType:= GetProcAddress(DLLInstance,functionname);
MyType(Sender, Notify);
FreeLibrary(DLLInstance);
end;

Then when you call AddItem, just pass the name of the function you want to call as a string parameter:

FirstClass.AddItem('MyProc');

This will work only if the parameter lists of all of the functions you want to call are identical, although a little creative coding could probably get around that.

-- The Smurf
Reply With Quote
  #3  
Old 01-11-2001, 10:26 AM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default RE: Passing functions as parameters

Look at this code posted by Cristian Ormazabal - DX-Squad:
NewsGroup: borland.public.delphi.objectpascal

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

type
TStringFunction = function: string;

procedure DoSomething(AFunc: TStringFunction);
begin
ShowMessage(AFunc);
end;

function StringFunc1: string;
begin
Result := 'This is the first function';
end;

function StringFunc2: string;
begin
Result := 'This is the second function';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DoSomething(StringFunc1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
DoSomething(StringFunc2);
end;

end.
Reply With Quote
  #4  
Old 01-11-2001, 10:29 AM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default Example 2

This code was posted by Glynn Owen on the newsgroup.
Newsgroup: borland.public.delphi.objectpascal

unit Unit1;

interface

uses
Windows, Messages, SysUtils, 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}

type Tpassablefunction = function(X,Y:Integer):String;

function Passable1(A,B:Integer):String;
Begin
If A*B < 1000
then Result := 'A*B < 1000'
Else Result := 'A*B >= 1000'
End;

function Passable2(C,D:Integer):String;
Begin
If C*D < 1000000
then Result := 'C*D < 1000000'
Else Result := 'C*D >= 1000000'
End;

procedure TForm1.Button1Click(Sender: TObject);
VAR X:array of TPassableFunction;
begin
Setlength(X,2);
X[0] := Passable1;
X[1] := Passable2;
ShowMessage(X[random(2)](Random(1000),Random(10000)))
end;

INITIALIZATION
Randomize
end.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.