Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

GetEnumerator method

Project: Resource File Unit

Unit: PJResFile.

Class: TPJResourceFile

Applies to: ~>1.1

function GetEnumerator: TPJResourceFileEnumerator;

Creates and returns a new enumerator of type TPJResourceFileEnumerator [~>1.1] that can enumerate the resources contained in a TPJResourceFile instance.

The purpose of GetEnumerator is to enable a TPJResourceFile instance to be enumerated in a for..in loop construct. In such cases GetEnumerator is called automatically and there is rarely any need call the method from code.

On compilers that don’t support for..in loops (i.e. Delphi 7 and earlier) you can call GetEnumerator from code to get an enumerator instance and use that to perform the enumeration (usually using a while loop). Once the enumeration is complete you must free the enumerator object. However, it is usually simpler to use the traditional for..do loop to iterate over the indexes of the available entries.

Example 1:

Here is how to iterate the resource entries in Delphi 2005 and later.

Assume there is a TPJResourceFile instance named ResFile and a method DoSomething that takes a TPJResourceEntry parameter.

var
  Entry: TPJResourceEntry;
  ...
begin
  ...
  for Entry in ResFile do
    DoSomething(Entry);
  ...
end;

Notice that neither GetEnumerator nor the methods of TPJResourceFileEnumerator [~>1.1] are called explicitly.

Example 2:

This example shows how to use the enumerator from code, as must be done when using Delphi 7 and earlier.

Assumes the ResFile object and the DoSomething method used in Example 1 are available.

var
  Enum: TPJResourceFileEnumerator;
begin
  ...
  Enum := ResFile.GetEnumerator;
  try
    while Enum.MoveNext do
      DoSomething(Enum.Current);
  finally
    Enum.Free;
  end;
end;