Solved

MethodAddress - How do I call the Method found

Posted on 1997-04-02
8
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 124
Delphi XE10 Round Image 2 230
QRReport  TQrmemo vertical stretching 1 68
Formating a integer number to float 2 10
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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