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!

Mimic the '&' (macro) function of VFP

Status
Not open for further replies.

mustangman96

Programmer
Apr 23, 2003
14
HU
How can I mimic the '&' (macro) function of the Visual FoxPro?

e.g.:
...
sProc='ProcedureToRun'
DO &sProc
...

This works in VFP and runs the 'ProcedureToRun' procedure.
Is it possible to do the same thing in Delphi?
 
Visual FoxPro is, I'm guessing, an interpreted language. Thus, using the mechanism you describe, you can pass a string to the interpreter, and ask it to find the function of that name and execute it.

Delphi is, of course, compiled. At compile time, the string names of functions have already been removed; so in Delphi we instead use function pointers. A common example of this in Delphi is the Compare used in various Sorts. Here's what Help on TList,Sort says:
Code:
type TListSortCompare = function (Item1, Item2: Pointer): Integer;
procedure Sort(Compare: TListSortCompare);

Description

Call Sort to sort the items in the Items array. Compare is a comparison function that indicates how the items are to be ordered.

So you write a Compare function of your own, depending on how objects in your particular list should be compared, and when you want to sort the list, you say:
Code:
  mylist.Sort(mycomparefunction);
In the more general case, you'll want to declare a type that describes the sort of functions/procedures you want to call, with their parameter lists and all, like:
Code:
type TMyFunction = function (myparameterlist): myreturntype;
, and then you can do with it all the things that you can usually do with a type: create variables of that type, pass objects of that type as function parameters, return objects of that type as function results, etc.
So you might say:
Code:
function myfunction1(myparameterlist): myreturntype;
begin
  Result := '1';
end;

function myfunction2(myparameterlist): myreturntype;
begin
  Result := '1';
end;
, then later:
Code:
var myfunctionvariable: TMyFunction;
begin
  myfunctionvariable := myfunction1;
  myfunctionvariable(myparamterlist);
end;
More help is available in help under under "Procedural types".

-- Doug Burbidge mailto:dougburbidge@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top