Accessing Interfaces

From: SimonW (bozzzza_at_lycos.co.uk)
Date: 06/25/04


Date: Fri, 25 Jun 2004 09:15:51 +0100

I was wondering what is the best way to access methods when using
interfaces?

I have seen two types of examples; which are listed below.

One of them uses the Interface. The other access the implementation class
method directly.
I guess that example2 would not use reference counting so I would have to
free the object myself?

TIA

Simon.

Type

  IInterfaceExample = interface(IInterface)
    ['{24868058-9C85-48C4-97E7-D0D730F61962}']
    function add(a,b : Integer) : Integer; stdcall;
  end;

   TInterFaceExample = class(TInterfacedObject,IInterfaceExample)
    function add(a,b : Integer) : Integer; stdcall;
  end;

 implementation

  function TInterfaceExample.add(a, b: Integer): Integer;
  begin
    result := a+b;
  end;

 var
   classInterfaceExample : TInterfaceExample;
   interInterfaceExample : IInterfaceExample;

  begin

   //Example 1 for accessing function
     interInterfaceExample := TInterfaceExample.create;
     answer := interInterfaceExample1.add(1,2);
     showMessage(format('%d',[answer]));

   //Example 2 for accessing function
     classInterfaceExample := TinterfaceExample.Create;
     answer := classInterfaceExample.add(1,2);
     showMessage(format('%d',[answer]));
     classInterfaceExample.Free;

 end;



Relevant Pages

  • Abstract Base Class means Abstract Composite/Aggregated Classes?
    ... Account Factory) with getAccount and updateAccount methods (among other ... Since the actual data access methods for the upgrades are so different there ... Am I wrong in that thinking that I will probably need to define interfaces ...
    (comp.object)
  • Re: Remoting a Reflected Object
    ... actually you can use interfaces - its just tricky to create the object ... an Interface cannot be inherited from MarshalByRefObj rite? ... > But May be I should inherit the Implementation class from MarshalByRefObj ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: compile-time error generating custom attributes
    ... Define interfaces. ... The implementation class can be discovered dynamically using reflection (say ... from config files). ...
    (microsoft.public.dotnet.framework)