Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to add Interfaces to a List Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
10-Nov-03
Category
OO-related
Language
Delphi 2.x
Views
133
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Max Kleiner

It's more efficient to control Interfaces in a List and ask with QueryInterface() 
which objects support an Interface 

Answer:

First we need some Interfaces (the same goes also in Kylix, pure Interfaces are 
independent from COM, it's a feature of ObjectPascal): 

//type
  IKiss = interface(IUnknown)
    ['{19A231B1-269F-45A2-85F1-6D8A629CC53F}']
    procedure kiss; stdcall;
  end;

  ISpeak = interface(IUnknown)
    ['{B7F6F015-88A6-47AC-9176-87B6E313962D}']
    procedure sayHello; stdcall;
  end;

//Second the interfaces must be implemented: 

TDog = class(TInterfacedObject, ISpeak)
public
  procedure sayHello; stdcall;
end;

TFrench = class(TInterfacedObject, ISpeak, IKiss)
public
  procedure kiss; stdcall;
  procedure sayHello; stdcall;
end;

TEnglish = class(TInterfacedObject, ISpeak)
public
  procedure sayHello; stdcall;
end;

//e.g. the dog with 

procedure TDog.sayHello;
begin
  showmessage('dog is barking wauwau');
end;

Now we add the instances of the interface in the list, using the defined type 
TInterfaceList so we are able to ask with QueryInterface if an object supports an 
Interface, in our example if a dog as an object can kiss or just sayhello: 
1   
2   procedure TForm1.btnCollectClick(Sender: TObject);
3   var
4     collection: TInterfaceList;
5     i: Integer;
6     aObjspeak: ISpeak;
7     aObjKiss: IKiss;
8   begin
9     collection := TinterfaceList.create;
10    try
11      with collection do
12      begin
13        add(TEnglish.create);
14        add(TFrench.create);
15        add(TDog.create);
16      end;
17      for i := 0 to collection.count - 1 do
18      begin
19        aObjSpeak := collection[i] as ISpeak; //TFrench, TEnglish, TDog
20        if aObjSpeak <> nil then
21          aObjSpeak.sayHello;
22        collection[i].queryInterface(IKiss, aObjKiss); //only TFrench
23        if aObjKiss <> nil then
24          aObjKiss.kiss;
25      end;
26    finally
27      collection.free;
28    end;
29  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC