I run into what seems to be a very classical problem: An item and a collection class, both referencing each other, that require a forward declaration. I\'m using Delphi 2010
My Collection's example (based on generics)
type
TMICustomItem = class(TPersistent)
private
FID: Variant;
FCollection: TList;
function GetContained: Boolean;
protected
procedure SetID(Value: Integer);
public
constructor Create(ACollection: TList); overload;
constructor Create(ACollection: TList; ID: Integer); overload;
procedure Add; //Adding myself to parent collection
procedure Remove; //Removing myself from parent collection
property Contained: Boolean read GetContained; //Check contains myself in parent collection
property ID: Variant read FID;
end;
TMICustomCollection = class(TList)
private
function GetItemByID(ID: Integer): ItemClass;
public
property ItemID[ID: Integer]: ItemClass read GetItemByID; //find and return Item in self by ID
end;
...
{ TMICustomItem }
constructor TMICustomItem.Create(ACollection: TList);
begin
FCollection := ACollection;
end;
constructor TMICustomItem.Create(ACollection: TList;
ID: Integer);
begin
Create(ACollection);
FID := ID;
end;
procedure TMICustomItem.Add;
begin
if not FCollection.Contains(Self) then
FCollection.Add(Self)
else
raise EListError.CreateRes(@SGenericDuplicateItem);
end;
procedure TMICustomItem.Remove;
begin
if FCollection.Contains(Self) then
FCollection.Remove(Self)
else
raise EListError.CreateRes(@SGenericItemNotFound);
end;
function TMICustomItem.GetContained: Boolean;
begin
Result := FCollection.Contains(Self);
end;
procedure TMICustomItem.SetID(Value: Integer);
begin
FID := Value;
end;
{ TMICustomCollection }
function TMICustomCollection.GetItemByID(ID: Integer): ItemClass;
var
I: Integer;
begin
for I := 0 to Count - 1 do
if Items[I].ID = ID then
Exit(Items[I]);
raise EListError.CreateRes(@SGenericItemNotFound);
end;