Call a method by Parameter

I want to call a method in a class using a parameter to specify the method to call. I don't want to mess around with Dispatch interfaces etc., as the class is part of my application. Does Delphi allow you to call a method like this ...
Eg.  ClassName.CallMethod('Method Name'); ?
sparkidoAsked:
Who is Participating?
 
King_DavidConnect With a Mentor Commented:
Thanks.
0
 
King_DavidCommented:
function HasMethod(Obj: TObject; const MethodName: ShortString): TMethod;
begin
  Result.Code := Obj.MethodAddress(MethodName);
  Result.Data := Obj;
end;

function CallMethod(Obj: TObject; const MethodName: ShortString): boolean;
// Method must be published.
// Ex: CallMethod(Self, 'MyMethod'); -> True / False.
type
  TMyProcedure = procedure of object;
var
  Method: TMethod;
begin
  Method := HasMethod(Obj, MethodName);
  if Assigned(Method.Code) then
    begin
    TMyProcedure(Method);
    Result := True;
    end
  else
    Result := False;
end;
0
 
ThaddyCommented:
What you can do is something like  this, provided the method has * NO* parameters!!!!!
DECLARE YOUR METHODS IN THE PUBLISHED SECTION< NOT THE PUBLIC SECTION!!!!!!

function executemethod(AnObject:Tobject;Aname:Shortstring);
begin
  AnObject.methodaddress(Aname);//pushes pointer in eax register
  asm
    cmp eax,nil
    je @exitlabel
    call eax
@exitlabel:
  end;

end;
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
sparkidoAuthor Commented:
David,

 This works OK, but how would I pass parameters to this (I know up front the parameter list) ?
0
 
simonetCommented:
Following...®

>how would I pass parameters to this ?

Two things come to my mind:

1)How about in the Stack?

2) Pscal/Delphi fuctions use EAX, EDX and EBX to receive parameters. But in the solution provided by Thaddy, EAX is already taken up, so EDX and EBX may be worthwhile trying.

Alex
0
 
King_DavidCommented:
Hi.
You have to declare the procedure type
with the parameters
Examle:
function CallMethod(Obj: TObject; const MethodName: ShortString; Param: String): boolean;
// Method must be published.
// Ex: CallMethod(Self, 'MyMethod', 'MyParam'); -> True / False.
type
  TMyProcedure = procedure (Param: String) of object;
var
  Method: TMethod;
begin
  Method := HasMethod(Obj, MethodName);
  if Assigned(Method.Code) then
    begin
    TMyProcedure(Method)(Param);
    Result := True;
    end
  else
    Result := False;
end;
0
 
sparkidoAuthor Commented:
Thanks very much David,

Submit as an answer. This is exactly what I was looking for.
0
 
King_DavidCommented:
I'm glad it helped you.
Since the question is locked I can not
submit it as an answer.
You have to reject the proposed
answer first.
0
 
sparkidoAuthor Commented:
I'd rather not have to include assembler code, as this makes it difficult for others to debug.
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0
All Courses

From novice to tech pro — start learning today.