1. Technology

Enumerators in Delphi - Creating Custom For-In Iterations

Understanding and Writing Enumerators and Iterators in Delphi

By

Introduced in Delphi 2005, the "for in" style iteration over containers was added to the Delphi language feature set.

The "for in" loop makes it easier to loop through the elements in a collection (e.g. characters in a string or objects in a TObjectList) by removing the need for the iterator variable, indexing each element and checking for boundaries.

An example using for in is:

var
  sList : TList<string>;
  s : string;
begin
  sList := TList<string>.Create;
  try
    sList.AddRange(['about', 'delphi', 'programming']);

    for s in sList do
      Caption := s;

  finally
    sList.Free;
  end;
end;
The above code iterates over the elements (strings) of an instance of the TList generic class.

If you look in the implementation of the TList class you will see a public function GetEnumerator. The GetEnumerator returns an "enumerator" which must be a class, interface or a record type. The enumerator exposes a MoveNext function and a Current property that returns the current element.

The Enumerator - GetEnumerator - Creating Your Own Enumerators

Writing enumerators for your own classes is simple. Have a class exposing MoveNext and Current and of you go. More or less :)

Anyway, here's a list of articles explaining how to create your own iterators to iterate over various "things"...

Enumerating Collections
Get to know what enumerators are and how they are implemented initially in Delphi 8 (for .NET)

Enumerables in Delphi
Languages such as C#, Java or Delphi have built-in syntax constructs that allow simplified and clean ways of enumerating over a collection by the means of “foreach” or special “for” loops.

Fun with enumerators - Introduction
Contains a short introduction on Delph iterators (for..in statement) and describes Delphi support for iterator extensibility.

Fun with enumerators - Additional enumerators
Shows how to add an additional enumerator to a class that already contains one.

Fun with enumerators - Parameterized enumerators
This chapter takes Part 2 topic one level further by introducing enumerator parameters.

Fun with enumerators - External enumerators
In this chapter you'll learn how to create enumerators without changing the class they are enumerating.

Fun with enumerators - Class helper enumerators
Shows how to create additional enumerators using class helpers and how to use same technique to add enumerators to classes that don't have one.

Fun with enumerators - Generators
The last chapter shows how to write enumerators that work on their own data, not on some external structure. It also includes full source code of a demo program and all enumerators described in the series.

Recursive component's enumerator in Delphi
I needed to loop through all the components in some forms, and when you have a frame inside a form, the default TComponent's enumerator doesn't return the components which are inside the frame.

More Fun With Enumerators
There are a number of ways to implement such enumerators. We will look at a couple of variants, studying the code that the compiler generates for them. We will mainly focus on getting as efficient code as possible.

.... +1 more reason to upgrade your Delphi 7 (or prior).

©2014 About.com. All rights reserved.