Board index » delphi » Converting a String into a procedure pointer

Converting a String into a procedure pointer

Hi all,
  I don't know if you can do this, but I have a table which contains
the names of procedures. I want to read my table, get the name of
the procedure, and assing that procedure to the onclick event of say a
button.  I guess that I could use the brute force and write something
like if myTable.FieldByName('xyz').AsString = 'Myprocedure' then
Button1.Click:=MyProcedure;  I could have 100 of these if statements,
but I don't like it and I was wondering if there is a way of doing just
this.

Thanks for your advice.
Jose R. Nebro
jo...@amanda.dorsai.org

 

Re:Converting a String into a procedure pointer


jo...@dorsai.dorsai.org (Jose R. Nebro) wrote:

Quote
>Hi all,
>  I don't know if you can do this, but I have a table which contains
>the names of procedures. I want to read my table, get the name of
>the procedure, and assing that procedure to the onclick event of say a
>button.  I guess that I could use the brute force and write something
>like if myTable.FieldByName('xyz').AsString = 'Myprocedure' then
>Button1.Click:=MyProcedure;  I could have 100 of these if statements,
>but I don't like it and I was wondering if there is a way of doing just
>this.

Delphi is a compiler and not an interpreter. The procedure names are
not known at runtime. You will have to write something like
if "blabla" then
  blabla
else if "blablabla" then
  blablabla;

But another approach could be:
type TMyProc = procedure or object;
var
  ProcArr : array [0 .. 100] of TMyProc;
  StrToProcString : String;

then assign all your procs to some places in this statement.
then create a string "StrToProcString" with the following approach.

StrToProcString := #1 + ProcName1 + #1 + ProcName2 + #1 + ProcName3 +
#1 + ProcName4 + .... + #1

During construction just remember the positions and map them to the
right functions in the array:
var
  Mapper : array[0 .. 100] of Integer;

The moment you recieve the procedure from your database calc the
position with:
  R := Pos (StrToProcString,  #1 + procfromdb + #1)

It must be found and the result will be >= 0
Search the array Mapper for the corresponding procedure.
----------------
Oooouch.....
This is very complicated, but it could be faster than this series of
ifs.

Re:Converting a String into a procedure pointer


jo...@dorsai.dorsai.org (Jose R. Nebro) wrote:

Quote
>Hi all,
>  I don't know if you can do this, but I have a table which contains
>the names of procedures. I want to read my table, get the name of
>the procedure, and assing that procedure to the onclick event of say a
>button.  I guess that I could use the brute force and write something
>like if myTable.FieldByName('xyz').AsString = 'Myprocedure' then
>Button1.Click:=MyProcedure;  I could have 100 of these if statements,
>but I don't like it and I was wondering if there is a way of doing just
>this.

You could use a string list with the  names of the procedures,
and store the procedures (in effect, their addresses) in the Objects
property of the list. Keeping the list in alphabetical order (Sorted
= true) will make things faster, I think.

--
Best wishes

John Nurick

e-mail: j.nur...@dial.pipex.com
v-mail: <+44|0> 191 281 1306

Re:Converting a String into a procedure pointer


Quote
Jose R. Nebro wrote:

> Hi all,
>   I don't know if you can do this, but I have a table which contains
> the names of procedures. I want to read my table, get the name of
> the procedure, and assing that procedure to the onclick event of say a
> button.  I guess that I could use the brute force and write something
> like if myTable.FieldByName('xyz').AsString = 'Myprocedure' then
> Button1.Click:=MyProcedure;  I could have 100 of these if statements,
> but I don't like it and I was wondering if there is a way of doing just
> this.

> Thanks for your advice.
> Jose R. Nebro
> jo...@amanda.dorsai.org

You could place the Links, Name<->procedure, in an array. This is the
only way to map them.

Bye,
Arjan

-----------------------------------
Arjan Lamaker
IMAG-DLO Wageningen The Netherlands
mail: e.j.j.lama...@imag.dlo.nl
http://huizen.dds.nl/~lamaker/
------------------------------

-----

Re:Converting a String into a procedure pointer


In <4pgglt$...@soap.news.pipex.net>, John Nurick <j.nur...@dial.pipex.com> writes:

Quote
>You could use a string list with the  names of the procedures,
>and store the procedures (in effect, their addresses) in the Objects
>property of the list. Keeping the list in alphabetical order (Sorted
>= true) will make things faster, I think.

>--

John,

 I'm trying to do something similar but using a generic List to hold the string/proc
pair.  ie;

type
  TOProcedure = procedure of object;

  TLookupItem = class(TObject)
    public
      constructor create(sNam: string; oProc: TOProcedure);
      function GetProcedure : TOProcedure;
    private
      sName: string;
      oProcedure: TOProcedure;
  end;

  TLookUpList = class(TList)
   public
     constructor Create;
     function MatchName(sNam: string) : TOProcedure;
   ....
   end;

The MatchName function searches through the list of TLookupItems until
it finds a name match.  Then it is supposed to return the TOProcedure to the
calling method.  However, I keep getting flagged with a 'Type mismatch error'
when I set TLookupList's MatchName method to TLookupItem's GetProcedure
method.

The types are the same.  What am I screwing up this time? ;-)

Richard

Re:Converting a String into a procedure pointer


74133.2...@compuserve.com        (Richard Knapp) wrote:

Quote
> I'm trying to do something similar but using a generic List to hold the string/proc
>pair.  ie;

>type
>  TOProcedure = procedure of object;

>  TLookupItem = class(TObject)
>    public
>      constructor create(sNam: string; oProc: TOProcedure);
>      function GetProcedure : TOProcedure;
>    private
>      sName: string;
>      oProcedure: TOProcedure;
>  end;

>  TLookUpList = class(TList)
>   public
>     constructor Create;
>     function MatchName(sNam: string) : TOProcedure;
>   ....
>   end;

>The MatchName function searches through the list of TLookupItems until
>it finds a name match.  Then it is supposed to return the TOProcedure to the
>calling method.  However, I keep getting flagged with a 'Type mismatch error'
>when I set TLookupList's MatchName method to TLookupItem's GetProcedure
>method.

>The types are the same.  What am I screwing up this time? ;-)

The result types of the functions are the same, but for the
functions to be type-compatible they must also have matching
parameter lists, which these do not.

(Or perhaps I'm missing something subtle).

--
Best wishes

John Nurick

e-mail: j.nur...@dial.pipex.com
v-mail: <+44|0> 191 281 1306

Re:Converting a String into a procedure pointer


In <4ppvgn$...@soap.news.pipex.net>, John Nurick <j.nur...@dial.pipex.com> writes:

Quote
>>type
>>  TOProcedure = procedure of object;

>>  TLookupItem = class(TObject)
>>    public
>>      constructor create(sNam: string; oProc: TOProcedure);
>>      function GetProcedure : TOProcedure;
>>    private
>>      sName: string;
>>      oProcedure: TOProcedure;
>>  end;

>>  TLookUpList = class(TList)
>>   public
>>     constructor Create;
>>     function MatchName(sNam: string) : TOProcedure;
>>   ....
>>   end;

>>The MatchName function searches through the list of TLookupItems until
>>it finds a name match.  Then it is supposed to return the TOProcedure to the
>>calling method.  However, I keep getting flagged with a 'Type mismatch error'
>>when I set TLookupList's MatchName method to TLookupItem's GetProcedure
>>method.

>The result types of the functions are the same, but for the
>functions to be type-compatible they must also have matching
>parameter lists, which these do not.

So you mean MatchName has the have the same parameters as TOProcedure?

Richard

Re:Converting a String into a procedure pointer


74133.2...@compuserve.com        (Richard Knapp) wrote:

Quote
>In <4ppvgn$...@soap.news.pipex.net>, John Nurick <j.nur...@dial.pipex.com> writes:
<snip>

>>The result types of the functions are the same, but for the
>>functions to be type-compatible they must also have matching
>>parameter lists, which these do not.

>So you mean MatchName has the have the same parameters as TOProcedure?

In the code snippet posted earlier, MatchName and GetProcedure
need to have matching parameter lists.

--
Best wishes

John Nurick

e-mail: j.nur...@dial.pipex.com
v-mail: <+44|0> 191 281 1306

Re:Converting a String into a procedure pointer


In <4q4j0r$...@soap.news.pipex.net>, John Nurick <j.nur...@dial.pipex.com> writes:

Quote
>74133.2...@compuserve.com        (Richard Knapp) wrote:

>>So you mean MatchName has the have the same parameters as TOProcedure?

>In the code snippet posted earlier, MatchName and GetProcedure
>need to have matching parameter lists.

John,

 Hmm.  Interesting.  I'll have to try that.  Thanks.

Richard

Re:Converting a String into a procedure pointer


Quote
> 74133.2...@compuserve.com        (Richard Knapp) wrote in article

<4q6njv$...@news.internetmci.com>...

Quote
> In <4q4j0r$...@soap.news.pipex.net>, John Nurick
<j.nur...@dial.pipex.com> writes:
> >74133.2...@compuserve.com        (Richard Knapp) wrote:

> >>So you mean MatchName has the have the same parameters as TOProcedure?

> >In the code snippet posted earlier, MatchName and GetProcedure
> >need to have matching parameter lists.

>  Hmm.  Interesting.  I'll have to try that.  Thanks.

I somehow missed the original post, but I gather the intent is to run a
function based on the function name stored in a string?  If so, I think I
have an idea.  Make all such functions that you want to run this way
exported, then use the GetProcAddr() API call with the module handle as
Application.Handle, and the ProcName PChar as the name of the procedure.
Though this API call is intended for getting procedures by name from DLLs
or device drivers, there is no reason why you couldn't use it on your own
EXE...

--
David S. Becker
ADP Dealer Services (Plaza R&D)
mailto:d...@plaza.ds.adp.com
(503) 402-3236

Re:Converting a String into a procedure pointer


On 10 Jun 1996 01:13:16 GMT, jo...@dorsai.dorsai.org (Jose R. Nebro)
wrote:

Quote
>  I don't know if you can do this, but I have a table which contains
>the names of procedures. I want to read my table, get the name of
>the procedure, and assing that procedure to the onclick event of say a
>button.  I guess that I could use the brute force and write something
>like if myTable.FieldByName('xyz').AsString = 'Myprocedure' then
>Button1.Click:=MyProcedure;  I could have 100 of these if statements,
>but I don't like it and I was wondering if there is a way of doing just
>this.

Probably the best way is to build your own data structure, and do your
own lookup, but a quick and dirty way is to define a class and make
all the methods published. Then you can call MethodAddress to look up
the procedures by name. Note that MethodAddress uses a linear search,
so if you need to look up the names often, you should write your own
lookup routine.
--
Ray Lischner                              li...@tempest-sw.com
Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

Other Threads