[fpc-pascal] TEnumerator - After the last element?

Gabor Boros mlnglsts at bgss.hu
Thu Oct 7 16:10:37 CEST 2021


Hi All,

I need to know the current position is after the last element. The 
attached solution demonstrate what I want.

Is an internal solution exists for this task? I not found anything.

Gabor
-------------- next part --------------
uses SysUtils, Generics.Collections;

type
  TR=record
    i:Integer;
  end;

  TLEnumerator=class(specialize TCustomListEnumerator<TR>)
  private
    function GetAfterLast:Boolean;
  public
    property AfterLast:Boolean read GetAfterLast;
  end;

  TL=class(specialize TList<TR>)
  public
    function GetEnumerator:TLEnumerator; reintroduce;
  end;

function TL.GetEnumerator:TLEnumerator;
begin
  Result:=TLEnumerator.Create(Self);
end;

function TLEnumerator.GetAfterLast:Boolean;
begin
  Result:=(FIndex>FList.Count-1);
end;

var
  R:TR;
  L:TL;
  E:TLEnumerator;

begin
  L:=TL.Create;
  R.i:=28;
  L.Add(R);
  R.i:=32;
  L.Add(R);
  E:=L.GetEnumerator;
  WriteLn(E.Current.i,'-',BoolToStr(E.AfterLast,'T','F'));
  E.MoveNext;
  WriteLn(E.Current.i,'-',BoolToStr(E.AfterLast,'T','F'));
  E.MoveNext;
  WriteLn(E.Current.i,'-',BoolToStr(E.AfterLast,'T','F'));
  E.MoveNext;
  WriteLn(E.Current.i,'-',BoolToStr(E.AfterLast,'T','F'));
end.


More information about the fpc-pascal mailing list