Giggler writes:
Quote
TClass3 is instanciated and referenced in an Interface3 type variable.
It is is also passed to a list of Interface1 Interfaces. When an
Interface1 instance is retrieved from the list, it cant be cast to
Interface3 or interface2. get Interface not supported error.
Can you give more information on the list of 'Interface1 Interfaces'?
Is it just a TInterfaceList?
Below is some code that seems to work with what I interpret you are
trying to achieve.. This is a from with a TButton on it that is
clicked.. (Just add the dfm)
Siegs
</Start>
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
IInterface1 = interface
['{06E16452-373C-4B43-BBBF-0D1D99D87DCA}']
function Method1: Integer; stdcall;
function Method2: Integer; stdcall;
end;
IInterface2 = interface
['{4B488B64-A345-4E96-B50F-3DEEC750DF30}']
function Method3: Integer; stdcall;
function Method4: Integer; stdcall;
end;
IInterface3 = interface
['{055918FF-FD57-4137-8379-1365FBF8025E}']
function Method5: Integer; stdcall;
function Method6: Integer; stdcall;
end;
TClass1 = class(TInterfacedObject, IInterface1)
function Method1: Integer; stdcall;
function Method2: Integer; stdcall;
end;
TClass2 = class(TInterfacedObject, IInterface1, IInterface2)
private
FInterface1: IInterface1;
public
constructor Create;
published
function Method3: Integer; stdcall;
function Method4: Integer; stdcall;
property Interface1: IInterface1 read FInterface1 implements
IInterface1;
end;
TClass3 = class(TClass2, IInterface3)
function Method5: Integer; stdcall;
function Method6: Integer; stdcall;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TClass3 }
function TClass3.Method5: Integer;
begin
Result := 0;
end;
function TClass3.Method6: Integer;
begin
Result := 0;
end;
{ TClass2 }
function TClass2.Method4: Integer;
begin
Result := 4;
end;
function TClass2.Method3: Integer;
begin
Result := 3;
end;
constructor TClass2.Create;
begin
inherited;
FInterface1 := TClass1.Create;
end;
{ TClass1 }
function TClass1.Method1: Integer;
begin
Result := 1;
end;
function TClass1.Method2: Integer;
begin
Result := 2;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aClass2: IInterface2;
aClass3: IInterface3;
InterfaceList : TInterfaceList;
begin
aClass2 := TClass2.Create;
aClass3 := TClass3.Create;
InterfaceList := TInterfaceList.Create;
try
InterfaceList.Add(aClass3);
MessageDlg(IntToStr((InterfaceList.Items[0] as
IInterface2).Method3), mtInformation, [mbOK], 0); //Displays '3'
MessageDlg(IntToStr((InterfaceList.Items[0] as
IInterface1).Method1), mtInformation, [mbOK], 0); //Displays '1'
finally
InterfaceList.Free;
end;
end;
end.
</>