Board index » delphi » Execute a function of which the name is in a string variable

Execute a function of which the name is in a string variable

Hello,

I have a list of functions (which are possible to execute by a user) in a
TMemo, these functions have the name of real functions in my Delphi code.
After a user makes a selection, I put the name of the selected function in a
string variable. But how can I execute this function then?

Thnkx

Gigi :-)

 

Re:Execute a function of which the name is in a string variable


If your App were a DLL then you could use LoadLibrary and
GetProcAddress

On Tue, 28 Aug 2001 00:23:44 +0200, "Giovanni Praet"

Quote
<g...@protelco.be> wrote:
>Hello,

>I have a list of functions (which are possible to execute by a user) in a
>TMemo, these functions have the name of real functions in my Delphi code.
>After a user makes a selection, I put the name of the selected function in a
>string variable. But how can I execute this function then?

>Thnkx

>Gigi :-)

Re:Execute a function of which the name is in a string variable


The straight forward way is to use a case statement having
enumerated your list of function (and function names):

The user selection ends up with a number indexing the
desired function name from the list. This number will
probably be between 0 and #names-1. Now you use:

case functionindex of
0 : process the first function;
1 : process the second function;
2 : process the third function;
...
#names-1 : process the last function;
else raise error for illegal index selection.
end;

hope this gives you an idea.

regards Sven

Quote
"Giovanni Praet" <g...@protelco.be> wrote in message

news:9mehej$30m$1@news.planetinternet.be...
Quote
> Hello,

> I have a list of functions (which are possible to execute by a user) in a
> TMemo, these functions have the name of real functions in my Delphi code.
> After a user makes a selection, I put the name of the selected function in
a
> string variable. But how can I execute this function then?

> Thnkx

> Gigi :-)

Re:Execute a function of which the name is in a string variable


Quote
> I have a list of functions (which are possible to execute by a user) in a
> TMemo, these functions have the name of real functions in my Delphi code.
> After a user makes a selection, I put the name of the selected function in
a
> string variable. But how can I execute this function then?

Here's one possible solution

function StringIndex(const SearchString : string; StrList : array of string)
: Integer;
var I : Integer;
begin
 Result := -1;
 for I := 0 to High(StrList) do
  if CompareText(SearchString, StrList[I]) = 0 then begin
   Result := I;
   Break;
  end;
end;

function StrFunction(FuncName : string) : boolean;
begin
 result := 0;
 case StringIndex(Uppercase(FuncName),['FUN1', 'FUNC2', 'SOME3RD']) of
  0 : DotheFirst;
  1 : DotheSecond;
  2 : AThirdFunction;
  else result := -1;
  end;
end;

Re:Execute a function of which the name is in a string variable


In article <9mehej$30...@news.planetinternet.be>, "Giovanni Praet"

Quote
<g...@protelco.be> writes:
>I have a list of functions (which are possible to execute by a user) in a
>TMemo, these functions have the name of real functions in my Delphi code.
>After a user makes a selection, I put the name of the selected function in a
>string variable. But how can I execute this function then?

Declare types defining the parameters of the functions. Associate the names and
references to the function in a list, and when the name is selected call the
reference. Something like ...

type
  {declare procedure types according to parameters}
  TProcA = procedure;
  TProcB = procedure(I : integer);
  TProcC = procedure(S : string);
  TFuncD = function : string;

{declare procedures}
procedure ProcZero;  // type TProcA
begin
  ShowMessage('Zero');
end;

procedure ProcOne(I : integer);   // type TProcB
begin
  ShowMessage('One - ' + IntToStr(I));
end;

procedure ProcTwo(S : string);   // type TProcC
begin
  ShowMessage('Two ' + S);
end;

function FunctionThree : string; // type TFuncD
begin
  Result := 'Here''s How';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  {add names and function references to listbox (could not be a Memo)}
  with ListBox1.Items do begin
    AddObject('ProcZero', TObject(@ProcZero));
    AddObject('ProcOne', TObject(@ProcOne));
    AddObject('ProcTwo', TObject(@ProcTwo));
    AddObject('FunctionThree', TObject(@FunctionThree));
  end;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  {extract reference, typecast and call with appropriate parameters}
  with ListBox1 do
    case ItemIndex of
      0 : TProcA(Items.Objects[ItemIndex]);
      1 : TProcB(Items.Objects[ItemIndex])(45);
      2 : TProcC(Items.Objects[ItemIndex])('Oranges');
      3 : ShowMessage(TFuncD(Items.Objects[ItemIndex]));
    end;
  {end; with ListBox1}
end;

Alan Lloyd
alangll...@aol.com

Re:Execute a function of which the name is in a string variable


Nice suggestion, but it defeats the purpose. You are back to a case
statement if your parameter lists are different. I would suggest that all
procedures take the same parameters, in which case you can do the following:

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  with ListBox1 do
      TProc(Items.Objects[ItemIndex]);
end;

If you want parameters, then standardise them.

--
John Bester
Oaktree Software
J...@Bester.net

Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20010829043041.05286.00002887@nso-mi.aol.com...
Quote
> In article <9mehej$30...@news.planetinternet.be>, "Giovanni Praet"
> <g...@protelco.be> writes:

> >I have a list of functions (which are possible to execute by a user) in a
> >TMemo, these functions have the name of real functions in my Delphi code.
> >After a user makes a selection, I put the name of the selected function
in a
> >string variable. But how can I execute this function then?

> Declare types defining the parameters of the functions. Associate the
names and
> references to the function in a list, and when the name is selected call
the
> reference. Something like ...

> type
>   {declare procedure types according to parameters}
>   TProcA = procedure;
>   TProcB = procedure(I : integer);
>   TProcC = procedure(S : string);
>   TFuncD = function : string;

> {declare procedures}
> procedure ProcZero;  // type TProcA
> begin
>   ShowMessage('Zero');
> end;

> procedure ProcOne(I : integer);   // type TProcB
> begin
>   ShowMessage('One - ' + IntToStr(I));
> end;

> procedure ProcTwo(S : string);   // type TProcC
> begin
>   ShowMessage('Two ' + S);
> end;

> function FunctionThree : string; // type TFuncD
> begin
>   Result := 'Here''s How';
> end;

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   {add names and function references to listbox (could not be a Memo)}
>   with ListBox1.Items do begin
>     AddObject('ProcZero', TObject(@ProcZero));
>     AddObject('ProcOne', TObject(@ProcOne));
>     AddObject('ProcTwo', TObject(@ProcTwo));
>     AddObject('FunctionThree', TObject(@FunctionThree));
>   end;
> end;

> procedure TForm1.ListBox1DblClick(Sender: TObject);
> begin
>   {extract reference, typecast and call with appropriate parameters}
>   with ListBox1 do
>     case ItemIndex of
>       0 : TProcA(Items.Objects[ItemIndex]);
>       1 : TProcB(Items.Objects[ItemIndex])(45);
>       2 : TProcC(Items.Objects[ItemIndex])('Oranges');
>       3 : ShowMessage(TFuncD(Items.Objects[ItemIndex]));
>     end;
>   {end; with ListBox1}
> end;

> Alan Lloyd
> alangll...@aol.com

Re:Execute a function of which the name is in a string variable


In article <999103099.775...@ftp.adept.co.za>, "John" <nom...@nomail.com>
writes:

Quote
>Nice suggestion, but it defeats the purpose. You are back to a case
>statement if your parameter lists are different. I would suggest that all
>procedures take the same parameters, in which case you can do the following:

Oh yes that makes it easier - it just depends just how wide a choice the user
has. And of course, the code I posted was just exemplary.

Alan Lloyd
alangll...@aol.com

Re:Execute a function of which the name is in a string variable


Quote
"Giovanni Praet" <g...@protelco.be> wrote in message

news:9mehej$30m$1@news.planetinternet.be...

Quote
> Hello,

> I have a list of functions (which are possible to execute by a user) in a
> TMemo, these functions have the name of real functions in my Delphi code.
> After a user makes a selection, I put the name of the selected function in
a
> string variable. But how can I execute this function then?

If all the procedures have the same parameter list signature then you can
use something like:

Type
    tMyProc = procedure (<parameters>);
    tDispatch = record
        ProcName : string;
        ProcAddr : tMyProc;
        end;

procedure P1 (<parameters>);
begin
. . .
end;

procedure P2 (<parameters>);
begin
. . .
end;
. . .
procedure Pn (<parameters>);
begin
. . .
end;

const
    cDispatchVector = array [1 .. N] of tDispatch =
        ((ProcName: 'P1'; ProcAddr: @P1),
         (ProcName: 'P2'; ProcAddr: @P2),
         . . .
        (ProcName: 'Pn'; ProcAddr: @Pn));

procedure RunIt (proc : string);

    function ProcIx (proc : string);

    begin
    result := N;
    while (result > 0) and (ANSICompareText (proc, cDispatchVector
[result].ProcName) <> 0) do
        dec (result);
    end;

begin
cDispatchVector [ProcIx (proc)].ProcAddr (<parameters>);
end;

Other Threads