Link to home
Start Free TrialLog in
Avatar of sheldon040297
sheldon040297

asked on

MethodAddress - How do I call the Method found

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;

Avatar of mheacock
mheacock

A code snippet would be helpful.

Also, why are you using MethodAddress to call the method?
Avatar of sheldon040297

ASKER

Edited text of question
TGenericMethod = procedure(StringIn : string);

should be

TGenericMethod = procedure(StringIn : string) of object;

Regards,

Erik.
This does not compile - Incompatible types: 'TGenericMethod'
and 'Pointer'.  What else needs to be added?
ASKER CERTIFIED SOLUTION
Avatar of sperling
sperling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
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.