You already know that I love all things enumeratorish. Just recently I found a great example that combines three kinds of Delphi magic to allow the programmer to write very simple TDataset enumeration loops.
[And for the sake of my life I can't remember where I first saw the link to this code. Apologies to the original author.] [Updated 2008-01-14: Jim McKeeth found my source. Apparently Nick Hodges was the first to blog about this enumerator. Thanks, Nick!]
This TDataset enumerator was written by Uwe Raabe and is freely available in the CodeGear's Code Central as item #25386.
The three trick he uses are:
- Data helper is used to push enumerator support into TDataset.
- Standard enumerator support to enumerate over dataset and return special descendant of the Variant type for each record.
- Custom variant type to automagically access record fields by by adding .RecordFieldName to the variant variable. [I must admit I didn't even know this was possible to do in Delphi.]
All together those tricks allow you to write very simple TDataset iteration loops (code taken from the example in the DN #25386):
for Employee in QuEmployee do begin
S := Trim(Format('%s %s', [Employee.First_Name, Employee.Last_Name]));
if Employee.Hire_Date < EncodeDate(1991, 1, 1) then
S := '*' + S;