I need to understand how to use the generic Delphi 2009 TObjectList
. My non-TObjectList
attempt looked like
TSomeClass = class(TObject)
private
FList1: Array of TList1;
FList2: Array of TList2;
public
procedure FillArray(var List: Array of TList1; Source: TSource); Overload;
procedure FillArray(var List: Array of TList2; Source: TSource); Overload;
end;
Here, TList1
and TList2
inherits the same constructor constructor TParent.Create(Key: string; Value: string);
. However, due to different specialization (e.g. different private fields), they will not be of the same type. So I have to write two nearly identical fill methods:
procedure TSomeClass.FillArray(var List: Array of TList1; Source: TSource);
begin
for i := 0 to Source.List1.Count - 1 do begin
SetLength(List, Length(List) + 1);
List[i] := TList1.Create(Source.List1[i].Key, Source.List1[i].Value);
end;
end;
with FillArray(List: Array of TList2; Source: TSource);
being identical, except for the replacement of TList1
with TList2
throughout. As far as I understand, this could be neatly circumvented by using TObjectList
and a single fill method; yet, I don't have a clue how to go about this. Do anyone have some good pointers on this? Thanks!