test

progging - To wander about and beg; to seek food or other supplies by low arts; to seek for advantage by mean shift or tricks.
progging - Programmer slang for writing computer code.

tirsdag 30. august 2011

Delphi Enumerable List - example

Just an example of a list of a specific type in delphi.
unit MyUtility;

interface

uses
  Classes,  {TList}
  Chart;    {TChart}

type
  TChartList = class;
  TChartListEnumerator = record
  private
    FIndex: Integer;
    FList: TChartList;
  public
    constructor Create(AList: TChartList);
    function MoveNext: Boolean; inline;
    function GetCurrent: TChart; inline;
    property Current: TChart read GetCurrent;
  end;
  TChartList = class(TList)
  public
    procedure Add(AChart: TChart);
    function GetItem(i:Integer): TChart;
    function GetEnumerator: TChartListEnumerator;
    property Items[i:Integer]: TChart read GetItem; default;
  end;

implementation

{TChartList}

procedure TChartList.Add(AChart: TChart);
begin
  inherited Add(AChart);
end;

function TChartList.GetItem(i:Integer): TChart;
begin
  Result := List[i];
end;

function TChartList.GetEnumerator: TChartListEnumerator;
begin
  Result := TChartListEnumerator.Create(Self);
end;

{ TChartListEnumerator }

constructor TChartListEnumerator.Create(AList: TChartList);
begin
  FIndex := -1;
  FList := AList;
end;

function TChartListEnumerator.GetCurrent: TChart;
begin
  Result := FList.List[FIndex];
end;

function TChartListEnumerator.MoveNext: Boolean;
begin
  Result := FIndex < FList.Count - 1;
  if Result then
    Inc(FIndex);
end;

end.
And it's use:
var
  Chart: TChart;
begin
  for Chart in ChartList do begin
    Chart.DoSomeThing();
  end;

Ingen kommentarer:

Legg inn en kommentar