Solved

MethodAddress - How do I call the Method found

Posted on 1997-04-02
8
243 Views
Last Modified: 2010-04-06
I have a class containing three procedures, each of which
expect a string as a parameter.  I can find the address
of the procedure to call by using MethodAddress.  However,
when I call the procedure using the address returned, and
pass a string, the value of the string is being passed as
empty to the procedure.

How do I overcome this?  I have been through the
documentation and source without luck.

The code below is a test of MethodAddress I have created
as an example.  The user enters a string in a text box and presses a button.  The first char entered is the procedure
to be executed, and the remaining characters are passed to
the procedure.

<SNIP - uses & form def>

  TGenericMethod = procedure(StringIn : string);

  TProcedures = class(TObject)
    procedure A(StringIn : string);
    procedure B(StringIn : string); virtual;
    procedure C(StringIn : string);
  end;

var
  Form1: TForm1;
  Procedures : TProcedures;

implementation

{$R *.DFM}

procedure TProcedures.A(StringIn : string);
begin
  MessageDlg('Procedure A - ' + StringIn, mtInformation, [mbOK], 0);
end;

procedure TProcedures.B(StringIn : string);
begin
  MessageDlg('Procedure B - ' + StringIn, mtInformation, [mbOK], 0);
end;

procedure TProcedures.C(StringIn : string);
begin
  MessageDlg('Procedure C - ' + StringIn, mtInformation, [mbOK], 0);
end;

procedure TForm1.btnRunProcClick(Sender: TObject);
var
  CallMethod : TGenericMethod;
  StringPar : string;
begin
  Procedures := TProcedures.Create;
  CallMethod := Procedures.MethodAddress(Copy(edtProcName.Text,1,1));
  StringPar  := Copy(edtProcName.Text,2,Length(edtProcName.Text) - 1);
  if Assigned(CallMethod) then
    CallMethod(StringPar)
  else
    MessageDlg('Procedure doesn''t exist', mtError, [mbOK], 0);
  Procedures.Free;
end;

0
Comment
Question by:sheldon040297
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:mheacock
ID: 1335106
A code snippet would be helpful.

Also, why are you using MethodAddress to call the method?
0
 

Author Comment

by:sheldon040297
ID: 1335107
Edited text of question
0
 
LVL 3

Expert Comment

by:sperling
ID: 1335108
TGenericMethod = procedure(StringIn : string);

should be

TGenericMethod = procedure(StringIn : string) of object;

Regards,

Erik.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:sheldon040297
ID: 1335109
This does not compile - Incompatible types: 'TGenericMethod'
and 'Pointer'.  What else needs to be added?
0
 

Author Comment

by:sheldon040297
ID: 1335110

0
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
ID: 1335111
Oops. Sorry, I forgot...

use a var
  Met   : TMethod

Met.Code := TProcedures.MethodAddress('A');
Met.Data := Procedures;

CallMethod := TGenericMethod(Met);

And then make the call...


Regards,

Erik.
0
 

Author Comment

by:sheldon040297
ID: 1335112
sperling's answer was exceptional!  But I could find no reference
to the steps used for the solution in the Delphi manuals.  If
anyone, in particular sperling, has a good reference for such
things I would really appreciate it (and wouldn't need to ask
questions here all the time).
0
 
LVL 3

Expert Comment

by:sperling
ID: 1335113
I usually look at the RTL/VCL source...

Docs? None that I know about... Might be in some 3rd-party Delphi book though...

I'd say you just need to spend some time to get acknowledged with the internal workings of the RTL/VCL. Read the source ;)


Regards,

Erik.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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