Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for in loop bug

Status
Not open for further replies.

Peca06

Programmer
Jun 3, 2008
2
0
0
CZ
I am using CodeGear Delphi 2007 for Win32,
look at the folowing code, the error is described at the end of text:

TAbstractEnumerator = class abstract
function GetCurrent: Integer; virtual; abstract;
function MoveNext: Boolean; virtual; abstract;
property Current: Integer read GetCurrent;
end;

TMyEnumerator = class(TAbstractEnumerator)
private
Index : integer;
public
function GetCurrent: Integer; override;
function MoveNext: Boolean; override;
property Current: Integer read GetCurrent;
constructor create;
end;

TAbstractList = class abstract
procedure Add; virtual; abstract;
function GetEnumerator : TAbstractEnumerator; virtual; abstract;
end;

TMyList = class(TAbstractList)
procedure Add; override;
function GetEnumerator : TAbstractEnumerator; override;
end;

constructor TMyEnumerator.create;
begin
Index := 0;
end;

function TMyEnumerator.GetCurrent: Integer;
begin
Result := Index;
end;

function TMyEnumerator.MoveNext: Boolean;
begin
Inc(Index);
Result := Index < 10;
end;

procedure TMyList.Add;
begin
inherited;
// do nothing
end;

function TMyList.GetEnumerator: TAbstractEnumerator;
begin
Result := TMyEnumerator.create;
end;

var List : TAbstractList;
i : integer;
Enumerator : TAbstractEnumerator;
begin
List := TMyList.Create;

//when I use the enumerator explicitly with while-loop, everything works fine

Enumerator := List.GetEnumerator;
while Enumerator.MoveNext do begin
Enumerator.Current;
end;

//but this code with for-in loop raises an EAbstractError - WHY???
for i in List do begin

end;
end;
 
What happens if you call it something other than "List"?

----------
Measurement is not management.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top