[fpc-pascal] Suggestion: TDataSetEnumerator
silvioprog
silvioprog at gmail.com
Mon Mar 30 05:04:27 CEST 2015
On Tue, Mar 10, 2015 at 5:48 PM, silvioprog <silvioprog at gmail.com> wrote:
> 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?
>
It was applied in trunk. Thanks guys! (y)
--
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/20150330/6d97a475/attachment.html>
More information about the fpc-pascal
mailing list