Does Delphi provide any nice way to iterate over TCollectionItems in a TCollection?
Something, perhaps, along the lines of...
for mycollectionitem in mycollection.Items do
mycollectionitem.setWhatever();
That doesn't compile though
or is there really nothing I can do that's more elegant than this:
for num := 1 to mycollection.Count do
mycollection.Items[num-1].setWhatever();
For..in
loops are implemented as calls toGetEnumerator
and the methods on the variable it returns. TheItems
property is not an object, but an array property that maps silently to a getter/setter pair, so it can't return an enumerator, butTCollection
itself does have aGetEnumerator
method.Thus:
Be aware, though, that
TCollection
is not a generic class, so the type of the enumerator index variable will beTCollectionItem
, and not whateverItemClass
you're working with.