Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Interface question

TiefTief Posts: 179Member
Hi,

I am a beginner with Delphi interfaces and I have a question.

Is it possible to know if a class implements a interface.
We can use "is" with classes and I was wondering if one similiar command is available with interfaces ?.

[code]
// example

type
TSomeClass = class(TObject, ISomeInterface)

...

var
aSomeClass : TSomeClass;

...

if (aSomeClass [blue]is[/blue] ISomeInterface) then ISomeInterface(aSomeClass).SomeIterfaceMethod;

...

[/code]

Thanks

Tief

Comments

  • MasterijnMasterijn Posts: 188Member
    Hello Tief,
    Each class has an interface table (possibly an empty one). An interface in this table is identified by its guid, and can be used to query for this interface.
    You can make a guid in Delphi with .
    It's also possible to query for another interface from an interface with IInterface.QueryInterface.
    At last there's a fast but static way. (See example) In this way the interface doesn't need a guid.
    [code]
    type
    IMyInterface = interface
    ['{6BD1F2DC-10F5-43E1-8898-BE61E61731C4}']
    procedure TestMethod;
    end;

    IMyInterface2 = interface
    ['{6CB82DFE-AD2B-4524-9D9C-9BBB141D8CC2}']
    procedure TestMethod2;
    end;

    IMyInterface3 = interface
    procedure TestMethod3;
    end;

    TMyObject = class(TInterfacedObject, IMyInterface, IMyInterface3)
    protected
    { IMyInterface }
    procedure TestMethod;
    { IMyInterface3 }
    procedure TestMethod3;
    end;

    implementation

    { TMyObject }

    procedure TMyObject.TestMethod;
    begin
    end;

    procedure TMyObject.TestMethod3;
    begin
    end;

    { TfrmMain }

    procedure TfrmMain.Button1Click(Sender: TObject);
    var
    MyObject: TMyObject;
    MyInterface: IMyInterface;
    MyInterface2: IMyInterface2;
    MyInterface3: IMyInterface3;
    begin
    { Getting a interface by querying the table: OK}
    MyObject := TMyObject.Create;
    if MyObject.GetInterface(IMyInterface, MyInterface) then
    begin
    Application.MessageBox('Interface implemented', '', MB_OK + MB_ICONINFORMATION);
    MyInterface.TestMethod;
    end;

    { Getting a interface by querying the table: Missing }
    if MyObject.GetInterface(IMyInterface2, MyInterface2) then
    begin
    Application.MessageBox('Interface2 implemented', '', MB_OK + MB_ICONINFORMATION);
    MyInterface2.TestMethod2;
    end;

    { Getting a interface by querying an interface: Missing }
    if MyInterface.QueryInterface(IMyInterface2, MyInterface2) = 0 then
    begin
    Application.MessageBox('Interface2 implemented', '', MB_OK + MB_ICONINFORMATION);
    MyInterface2.TestMethod2;
    end;

    { Getting a interface the static way }
    MyInterface3 := MyObject;
    end;

    [/code]


Sign In or Register to comment.