title: Delphi Generics method
body: <p>I am using generics to define a list of objects that themselves hold generic lists. I have written a method for retrieving the aggregate of each of these lists using specific methods that basically do the same thing. Below is the structure:</p>
  
  <pre><code>unit Unit1;
  
  interface
  
  uses
   System.Generics.Collections;
  
  type
    TListType=(ltSType,ltFType);
  
    TMyList&lt;T&gt; = class(TList&lt;T&gt;)
     {Do some stuff in here to help load lists etc}
    end;
  
    TListObject=class(TObject)
    private
      FSList:TMyList&lt;string&gt;;
      FIList:TMyList&lt;integer&gt;;
      function GetSList: TMyList&lt;string&gt;;
      function GetIList: TMyList&lt;integer&gt;;
    public
      property MySList:TMyList&lt;string&gt; read GetSList;
      property MyIList:TMyList&lt;integer&gt; read GetIList;
      constructor create;
    end;
  
    TListOfObject&lt;T:TListObject&gt; = class(TObjectList&lt;T&gt;)
    public
      Function AggrSList:TMyList&lt;string&gt;;
      Function AggrIList:TMyList&lt;integer&gt;;
    end;
  
  implementation
  
  { TListObject }
  
  constructor TListObject.create;
  begin
    FSList:=TMyList&lt;string&gt;.create;
    FIList:=TMyList&lt;integer&gt;.create;
  end;
  
  function TListObject.GetIList: TMyList&lt;integer&gt;;
  begin
    result:=FIlist;
  end;
  
  function TListObject.GetSList: TMyList&lt;string&gt;;
  begin
    result:=FSList;
  end;
  
  
  { TListOfObject&lt;T&gt; }
  
  function TListOfObject&lt;T&gt;.AggrIList: TMyList&lt;integer&gt;;
  var
    i,j:integer;
  
  begin
    result:=TMyList&lt;integer&gt;.create;
    for I := 0 to count-1 do
      for j := 0 to items[i].MyIList.Count-1 do
        result.Add(items[i].MyIList[j]);
  end;
  
  function TListOfObject&lt;T&gt;.AggrSList: TMyList&lt;string&gt;;
  var
    i,j:integer;
  begin
    result:=TMyList&lt;string&gt;.create;
    for I := 0 to count-1 do
      for j := 0 to items[i].MySList.Count-1 do
        result.Add(items[i].MySList[j]);
  end;
  
  end.
  </code></pre>
  
  <p>I am still fairly new to generics but feel that the aggregate methods AggrIList and AggrSlist could be written using generics that uses a single method to extract the data then is cast at the result. </p>
  
  <p>Is this possible and how would I approach this? I plan to do some more advanced functions that would benefit from this approach as well.  </p>