boardtc
asked on
Pass either function OR procedure in same argument
I woudl like one argument in a procedure to be able to accept either a pocedure OR a function that returns a string. Is there a way of doing this?
Thanksm Tom.
Thanksm Tom.
ASKER
Thanks for the response. To clarify what I am looking for is to have one procedure be able to accept either a function or a procedure as an arguement rather than having 2 overloaded procedures.
Cheers, Tom.
Cheers, Tom.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with Cesario.
Look at this ;)
function a(i:integer):string;
begin
Result:=inttostr(i);
end;
procedure b(var i:integer);
begin
i:=i+1;
end;
procedure GetSome(p:pointer;var s:string);
var i:integer;
begin
i:=2;
if p=@a then
s:=a(i);
if p=@b then begin
b(i);
s:=inttostr(i);
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
var s:string;
begin
GetSome(@a,s);
showmessage('func a '+s);
GetSome(@b,s);
showmessage('proc b '+s);
end;
Look at this ;)
function a(i:integer):string;
begin
Result:=inttostr(i);
end;
procedure b(var i:integer);
begin
i:=i+1;
end;
procedure GetSome(p:pointer;var s:string);
var i:integer;
begin
i:=2;
if p=@a then
s:=a(i);
if p=@b then begin
b(i);
s:=inttostr(i);
end;
end;
procedure TForm1.Button1Click(Sender
var s:string;
begin
GetSome(@a,s);
showmessage('func a '+s);
GetSome(@b,s);
showmessage('proc b '+s);
end;
It's no problem to pass these things to one function, you can just pass the address:
procedure GiveMeAProcOrFunc(procOrFu nc: pointer);
GiveMeAProcOrFunc(@somePro c);
GiveMeAProcOrFunc(@stringR eturningFu nc);
However, the big problem is: What do you want to do with the procedure/function pointer once you got it in the "GiveMeAProcOrFunc" procedure? Do you want to *call* it? Then you somehow have to find out which parameters the function has, which calling convention and which result type (if any).
Could you please tell us for what purpose you need this exactly? Perhaps we can make alternative suggestions...
Regards, Madshi.
procedure GiveMeAProcOrFunc(procOrFu
GiveMeAProcOrFunc(@somePro
GiveMeAProcOrFunc(@stringR
However, the big problem is: What do you want to do with the procedure/function pointer once you got it in the "GiveMeAProcOrFunc" procedure? Do you want to *call* it? Then you somehow have to find out which parameters the function has, which calling convention and which result type (if any).
Could you please tell us for what purpose you need this exactly? Perhaps we can make alternative suggestions...
Regards, Madshi.
ASKER
Thanks for that. I am basically trying to avoid a try finally wrapper than is duplicated in a bunch of class methods and procedures (you of course can't pass class method pointers). It's not involved enough to justify an inheritance / strategy pattern solution so I guess I'll just have to suck it up :-)
Cheers, Tom.
Cheers, Tom.
TStringFunc=function(x:dou
TSomeProc=procedure(var i:integer);
procedure GetSome(func:TStringFunc);
procedure GetSome(proc:TSomeProc);ov
implementation
procedure GetSome(func:TStringFunc);
begin
//
end;
procedure GetSome(proc:TSomeProc);ov
begin
//
end;