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):
var
Employee: Variant;
S: string;
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;
end;
Excellent job!
This was news to me, too. Great stuff! I'll have to try it out sometime. :)
ReplyDeleteVery usefull. I needed that. Many thanks.
ReplyDeleteNick Hodges had a link to it. Really cool stuff.
ReplyDeletePretty nice stuff, it can be improved as:
ReplyDelete-Instead of TBookmark use TBookmarkStr and then change the destructor with :
"if FBookmark <> '' then
FDataSet.Bookmark := FBookmark;"
So it does not crash when empty or modify
-Also is a good idea to raise an exception when the field does not exists.
Thanks
@jim: So that was my source. Thanks!
ReplyDelete@anonymous: You should post those comments to the CodeCentral. I'm no the author of this enumerator.