whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

71 Visitors Online


 
...add interface-objects to a list?
Autor: max kleiner
Homepage: http://max.kleiner.com
[ Print tip ]  

Tip Rating (4):  
     


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

  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:
}

procedure TForm1.btnCollectClick(Sender: TObject);
var
  
collection: TInterfaceList;
  i: Integer;
  aObjspeak: ISpeak;
  aObjKiss: IKiss;
begin
  
collection := TinterfaceList.Create;
  try
    with 
collection do 
    begin
      
Add(TEnglish.Create);
      Add(TFrench.Create);
      Add(TDog.Create);
    end;
    for i := 0 to collection.Count - 1 do 
    begin
      
aObjSpeak := collection[i] as ISpeak; //TFrench, TEnglish, TDog
      
if aObjSpeak  nil then
        
aObjSpeak.sayHello;
      collection[i].queryInterface(IKiss, aObjKiss); //only TFrench
      
if aObjKiss  nil then
        
aObjKiss.kiss;
    end;
  finally
    
collection.Free;
  end;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners