?
Solved

Pass either function OR procedure in same argument

Posted on 2002-05-20
6
Medium Priority
?
131 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:tomcorcoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:MBo
ID: 7021618
type
  TStringFunc=function(x:double):string;
  TSomeProc=procedure(var i:integer);

procedure GetSome(func:TStringFunc);overload;
procedure GetSome(proc:TSomeProc);overload;

implementation

procedure GetSome(func:TStringFunc);
begin
//
end;

procedure GetSome(proc:TSomeProc);overload;
begin
//
end;

0
 

Author Comment

by:tomcorcoran
ID: 7021696
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.
0
 
LVL 8

Accepted Solution

by:
Cesario earned 200 total points
ID: 7022320
Dear tom,

AFAIK, its not possible.

Best Regards

Cesario
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:MBo
ID: 7023163
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;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7023355
It's no problem to pass these things to one function, you can just pass the address:

procedure GiveMeAProcOrFunc(procOrFunc: pointer);

GiveMeAProcOrFunc(@someProc);
GiveMeAProcOrFunc(@stringReturningFunc);

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

Author Comment

by:tomcorcoran
ID: 7025169
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.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question