Well after a few minutes of finger bluring action last night I got it
working with MethodAccess.  I've included an example below -

{-------------------------------------------------------------}

type

  TMathOperation = function (const iVar1, iVar2 : integer) : integer of
object;

  TBasicMath = class(TObject)
  private
  protected
  public
    class function Execute(const sOperation : string; const iVar1, iVar2 :
integer) : integer;

  published

    function Add(const iVar1, iVar2 : integer) : integer;
    function Subtract(const iVar1, iVar2 : integer) : integer;
    function Multiply(const iVar1, iVar2 : integer) : integer;
    function Divide(const iVar1, iVar2 : integer) : integer;

  end;

implementation

{$R *.DFM}

class function TBasicMath.Execute(const sOperation : string; const iVar1,
iVar2 : integer) : integer;
var
  ABasicMath : TBasicMath;
  AMathOp : TMathOperation;
  AMathMethod : TMethod;
begin

  ABasicMath := TBasicMath.Create;
  try

    AMathMethod.Code := ABasicMath.MethodAddress(sOperation);
    AMathMethod.Data := ABasicMath;
    AMathOp := TMathOperation(AMathMethod);

    assert(assigned(AMathOp));

    Result := AMathOp(iVar1, iVar2)

  finally
    ABasicMath.Free;
  end;

end;

function TBasicMath.Add(const iVar1, iVar2 : integer) : integer;
begin
  Result := iVar1 + iVar2;
end;

function TBasicMath.Subtract(const iVar1, iVar2 : integer) : integer;
begin
  Result := iVar1 - iVar2;
end;

function TBasicMath.Multiply(const iVar1, iVar2 : integer) : integer;
begin
  Result := iVar1 * iVar2;
end;

function TBasicMath.Divide(const iVar1, iVar2 : integer) : integer;
begin
  Result := iVar1 div iVar2;
end;

{-------------------------------------------------------------}

The usage for it would be something like this -

Edit1.Text := IntToStr(TBasicMath.Execute('Divide', 81, 9));


Hopefully its useful for someone.

Cheers,
Nahum.

> -----Original Message-----
> From: Stephen Bertram [mailto:[EMAIL PROTECTED]]
> Sent: Friday, April 06, 2001 09:33 AM
> To: Multiple recipients of list delphi
> Subject: RE: [DUG]: How to Do This
> 
> 
> Further to my earlier post I found some of the code I used to 
> call functions
> by name at runtime and have put this below.  The basic idea 
> (and the bulk of
> the code) for this was given to me by a C programmer.
> 
> If anyone has a neater way to access methods at runtime I 
> would love to see
> it.
> 
> Stephen
> 
> In the Application units
> -----------------------------------
> ...
> uses dispatch;
> ...
> var
>   Req : TStringList;
>   LocalVariable : String;
> ...
>   // To call a function - initialize the parameters and call CallFnc
>   Req.Clear;
>   Req.Add('Param1=XX');
>   Req.Add('Param2=1');
>   LocalVariable := CallFnc('FunctionName',Req);
> ...
> initialization
>   // Add all functions to the FnList
>   FnList.AddObject('<FunctionName>=<whatever ID you want to
> use>',@FunctionName);
> end.
> 
> 
> Function calling unit
> ------------------------------------ 
> unit dispatch;
> 
> interface
> 
> uses Classes, sysutils;
> 
> type
>    TCallFn = function(req : TStringList) : string;
> 
> function CallFnc (FncName : string; Req : TStringList):string;
> 
> var
>    FnList : tstringlist;
> 
> implementation
> 
> function CallFnc (FncName : string; Req : TStringList):string;
> begin
>    if Fnlist.indexofname(FncName) = -1 then
>       Result := ' Function ' + FncName + ' not found '
>    else
>       Result :=  TCallFn (FnList.objects[
> FnList.indexofname(FncName)])(Req);
> end;
> 
> initialization
>   FnList := TStringList.Create;
> finalization
>   FnList.Free;
> end.
> --------------------------------------------------------------
> -------------
>     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED] 
> with body of "unsubscribe delphi"
> 
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to