[fpc-pascal] Suggestion: TDataSetEnumerator

silvioprog silvioprog at gmail.com
Tue Mar 10 21:48:54 CET 2015


On Sat, Mar 7, 2015 at 4:27 AM, LacaK <lacak at zoznam.sk> wrote:

> What if TDataSetEnumerator.Current will return TFields instead of TDataSet
> which enumerates?
>
> Example:
>   procedure IterateDataSet;
>   var DataSet, DS: TDataSet;
>       Flds: TFields;
>       F: TField;
>   begin
>     for Flds in DataSet do
>       for F in Flds do  // DataSet can be still accessed by Flds.DataSet
>         writeln(F.AsString,'|');
>   end;
>
> I know that it is more or less same ... it depends what will be most
> common usage scenario.
> In one case it will be better have DataSet in another Fields.
> When iterating DataSet IMO only Fields, Recno, Bookmark are usable
> properties which changes on each itteration and can be practically used
> inside loop.
> But I think, that most common scenario will be use only Fields inside loop.
> In both cases user must define "control variable" either of TDataSet type
> or TFields type.
>
> (*** Example implementation ***)
> type
>   TDataSetEnumerator = class
>     private
>       FDataSet: TDataSet;
>       FBOF: Boolean;
>       function GetCurrent: TFields;
>     public
>     &nb sp; constructor Create(ADataSet: TDataSet);
>       function MoveNext: Boolean;
>       property Current: TFields read GetCurrent;
>   end;
>
> operator Enumerator(ADataSet: TDataSet): TDataSetEnumerator;
> begin
>   Result := TDataSetEnumerator.Create(ADataSet);
> end;
>
> constructor TDataSetEnumerator.Create(ADataSet: TDataSet);
> begin
>   inherited Create;
>   FDataSet := ADataSet;
>   FBOF := True;
>   FDataSet.First;
> end;
>
> function TDataSetEnumerator.GetCurrent: TFields;
> begin
>   Result := FDataSet.Fields;
> end;
>
> function TDataSetEnumerator.MoveNext: Boolean;
> begin
>   if FBOF then
>     FBOF := False
>   else
>     FDataSet.Next;
>   Result := not FDataSet.EOF;
> end;
>
> (* *** *)
>
> -Laco.
>

Very nice. I open a new issue about that (
http://bugs.freepascal.org/view.php?id=27644). =)

I have a question. Can I use two or more enum operators in a same unit?

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150310/3b461a7d/attachment.html>


More information about the fpc-pascal mailing list