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
Solved

MethodAddress - How do I call the Method found

Posted on 1997-04-02
8
244 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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