I have the following code to hand enumeration over to a private field:
interface
type
TMyClassEnumerator = class(TEnumerator<TMyClass>)
end;
TMyContainerClass = class(TObject)
private
FItems: TObjectDictionary<string, TMyClass>;
public
function GetEnumerator: TMyClassEnumerator;
end;
implementation
function TMyContainerClass.GetEnumerator: TMyClassEnumerator;
begin
Result := TMyClassEnumerator(FItems.Values.GetEnumerator);
end;
Am I doing this right? I fought with implementing IENumerable<TMyClass> or creating my own interface, however, I got none of it to compile… The above code does work for example with for..in, but I feel I should make it a proper enumerable by implementing such interface.
Your approach is sound, but I don’t see that you need to cast, or indeed declare an enumerator class. I’d do it like this: