I have a parent interface (IParent
), a child interface (IChild
) and an object which implements the child interface.
I'm trying to call a function which accepts an array of IParent
parameter by passing an array of objects implementing the child interface.
On compiling I get the following error:
[dcc32 Error] Unit1.pas(46): E2010 Incompatible types: 'IParent' and 'TForm1'
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
IParent = interface
procedure DoSomething();
end;
IChild = interface(IParent)
procedure DoSomethingElse();
end;
TForm1 = class(TForm, IChild)
procedure FormCreate(Sender: TObject);
public
procedure DoSomething();
procedure DoSomethingElse();
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure CallAllDoSomething(AArray : array of IParent);
var
i : integer;
begin
i := 0;
while(i < Length(AArray)) do
begin
AArray[i].DoSomething();
Inc(i);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Unit1.CallAllDoSomething([Self]);
end;
procedure TForm1.DoSomething();
begin
ShowMessage('Something');
end;
procedure TForm1.DoSomethingElse();
begin
ShowMessage('Something else');
end;
end.