Reputation: 4178
Using Delphi 7. Here is an (incomplete) example that demonstrates my problem:
interface
uses Classes, Contnrs;
type
IEditorModule = interface;
procedure Method1;
procedure Method2;
end;
TEditorModuleList = class(TList)
protected
function GetItem(Index: Integer): IEditorModule;
procedure SetItem(Index: Integer; const Value: IEditorModule);
public
property Items[Index: Integer]: IEditorModule
read GetItem write SetItem; default;
end;
implementation
function TEditorModuleList.GetItem(Index: Integer): IEditorModule;
begin
Result := IEditorModule(inherited Items[index]);
end;
Cannot compile this because I get this error:
[Error] LEditorModule.pas(73): Incompatible types: 'IEditorModule' and 'TObject'
The main reason for declaring a new TList descendant is to be able to do things like:
aModuleList[3].Method1;
What kind of syntax will allow me to cast an object to an interface (instead of a concrete class)? Facts:
How do I do this?
Upvotes: 2
Views: 479
Reputation: 6587
The easiest thing to do is to use TInterfaceList for interfaces. It is available in Delphi 7. There is some logic built into TInterfaceList to manage reference counts for example setting them to nil them when clearing the list.
If you look at the code behind TInterfaceList you will see some of the actions that take place.
One thing you should be a bit careful of is that TInterfaceList uses TThreadList internally so there is a bit of overhead where it locks and unlocks the list.
Upvotes: 6