Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Starting a Procedure or Function

Status
Not open for further replies.

mustangman96

Programmer
Apr 23, 2003
14
HU
I want to start a procedure or function which name is in a variable as a string.

e.g.:
var sProcToCall : string;
.
.
begin
sProcToCall:='IwantToCallThis'; // The name of the procedure
.
.
// and here I want to call the procedure

Please help me!



 
First define a procedure type and then declare a procedure variable. For example, if you had a function which took a string parameter and returned an integer you would define the procedure type and declare the variable as follows:
Code:
type
  TIntFunction = function ( s: string ): integer;
var
  f: TIntFunction;
Later on in your code you could assign the name of a function to the variable f and then actually call f:
Code:
function FunctionX ( xxx: string ): integer;
begin
  result := 42;
end;

begin
  ...
  ...
  f := FunctionX;
  ...
  ShowMessage ( IntToStr ( f('Test') ) );
  ...
end;
This should get you started. The Delphi documentation is quite good in this area.

Procedures are handled in a similar way to functions.

Andrew
 
Thanks but I think this doesn't help.
I want to mimic the '&' (macro) function of the Visual FoxPro.
In FoxPro:
.
sProcToCall='IwantToCallThis'
DO &sProcToCall
.
works.
 
I'm thinking there's a way to achieve what you're wanting to do, but not the way you're hoping to.

First up, there's essentially no way to map a String to a procedure or function name. This is because procedures and functions are pointers, whereas Strings are not. There's no global procedure list that can be searched. And (although I may be wrong) the compiled program doesn't actually contain the names of your procedures and functions anyway. So you can see how it's impossible that way.

The workaround is define a single function that is hardcoded to call your specific functions. ie.

function StringToFunction(AFunction: String): Variant;
begin
if AFunction = 'FunctionX' then Result := FunctionX;
if AFunction = 'FunctionY' then Result := FunctionY;
{ etc. }
end;

This is (in my opinion) pretty ugly code, and is quite limiting on parameter passing. It can be cleaned up a little bit, and enhanced to allow parameters if you wanted to.

I don't know your reasons for wanting to do this, but my suggestion would be pretend you can't and focus on removing this requirement from your code structure.
 
I have a graphic menu system (developed in VFP) which uses data in a text file to build itself.

e.g.:
The content of the text file:
.
.
[MENU_1}
MenuCaption1;CallThisProc1;Param1;Param2
MenuCaption2;CallThisProc2
MenuCaption3;CallThisProc3;Param1
.
.

I would like to rewrite this menu system in Delphi.
 
Ok, just done some playing around, and I think I've got a prettier solution for you. I haven't actually compiled anything, so this could be a spectacular failure - but on the surface it all looks good.

Set up an ActionList (on the Standard component toolbar).

Right-click the ActionList and choose the editor. Now set up an action for every type of menuitem you'll need to support. Each action can be assigned on OnExecute event, which you link to the correct procedure in your code.

Then, when you dynamically create your menu, you create each menuitem, and then assign it's Action property to the correct item in the ActionList. Because each ActionList can carry a caption, you now have a text string you can search for.

Hope that makes sense. Post back if you want some more help, or if I haven't been clear - and let us know if it works for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top