Solved

MethodAddress - How do I call the Method found

Posted on 1997-04-02
8
237 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
Comment Utility
A code snippet would be helpful.

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

Author Comment

by:sheldon040297
Comment Utility
Edited text of question
0
 
LVL 3

Expert Comment

by:sperling
Comment Utility
TGenericMethod = procedure(StringIn : string);

should be

TGenericMethod = procedure(StringIn : string) of object;

Regards,

Erik.
0
 

Author Comment

by:sheldon040297
Comment Utility
This does not compile - Incompatible types: 'TGenericMethod'
and 'Pointer'.  What else needs to be added?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sheldon040297
Comment Utility

0
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now