That's much nicer than my method as you don't have to predetermine the
format of the parameters or remember to add them to the list.  I knew there
had to be a way but missed the  MethodAddress option.

Thanks 

Stephen

-----Original Message-----
From: Nahum Wild [mailto:[EMAIL PROTECTED]]
Sent: Friday, 6 April 2001 9:36 a.m.
To: Multiple recipients of list delphi
Subject: RE: [DUG]: How to Do This


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.
---------------------------------------------------------------------------
    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