Solved

Pass either function OR procedure in same argument

Posted on 2002-05-20
6
122 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
6 Comments
 
LVL 1

Expert Comment

by:MBo
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
Dear tom,

AFAIK, its not possible.

Best Regards

Cesario
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:MBo
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now