在DELPHI没有FOR IN的语法时,我们要使用如下代码枚举数据集中的每个内容:

cds.First;

while not cds.eof do begin

 ...

 cds.Next;

end;

最少要四句,而且字数有些多.如果程序中大量使用这个,代码看起来总是不舒服.

在DELPHI XE,可以利用XE对 FOR IN 扩展,建立新的扩展,使用起来好象有点动态语言的味道:

unit faDataSetEx;

interface

uses
DB,ADODB,Variants,SysUtils,StrUtils,Controls,System.Types,Classes,DBClient; type TFieldsEx = class helper for TFields
private
function GetFV (Index :String) :Variant;
procedure SetFV (Index :String; const Value :Variant);
public
property FV[Index :String] :Variant read GetFV write SetFV; default;
end; TDataSetEnumerator = class
private
FFirst :boolean;
FDataSet :TDataSet;
function GetCurrent :TFields;
public
constructor Create (dataset :TDataSet); function MoveNext :boolean;
property Current :TFields read GetCurrent;
end; TDataSetEx = class helper for TDataSet
public
function GetEnumerator :TDataSetEnumerator;
end; implementation { TDataSetEx } function TDataSetEx.GetEnumerator :TDataSetEnumerator;
begin
Result := TDataSetEnumerator.Create (Self);
end; { TDataSetEnumerator } constructor TDataSetEnumerator.Create (dataset :TDataSet);
begin
FDataSet := dataset; FFirst := True; end; function TDataSetEnumerator.GetCurrent :TFields;
begin
Result := FDataSet.Fields;
end; function TDataSetEnumerator.MoveNext :boolean;
begin
if FFirst then begin
FFirst := False;
FDataSet.First;
end
else
FDataSet.Next; Result := not FDataSet.eof;
end; { TFieldsEx } function TFieldsEx.GetFV (Index :String) :Variant;
begin
Result := FieldByName (Index).Value;
end; procedure TFieldsEx.SetFV (Index :String; const Value :Variant);
begin
FieldByName (Index).Value := Value;
end; end.

faDataSetEx

有了上面单元的代码,我们可以这样枚举数据集的每个行:

var
d :TFields;
begin
for d in cds1 do begin
cds1.edit;
d['a']:='abc';
cds1.post;
end;
end;

相当方便........

使用for in 循环数据集的更多相关文章

  1. VB6.0 读取CSV文件

    最近做了一个Upload文件的需求,文件的格式为CSV,读取文件的方法整理了一下,如下: 1.先写了一个读取CSV文件的Function: '读取CSV文件 '假设传入的参数strFile=C:\Do ...

  2. C# 处理Word自动生成报告 三、设计模板

    C# 处理Word自动生成报告 一.概述 C# 处理Word自动生成报告 二.数据源例子 C# 处理Word自动生成报告 三.设计模板 C# 处理Word自动生成报告 四.程序处理 既然是模板就少不了 ...

  3. pytorch例子学习——TRAINING A CLASSIFIER

    参考:https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar1 ...

  4. C#MySQL增删改查

    首先在项目中添加引用 using MySql.Data.MySqlClient; 连接字符串  private string connString="server=localhost;use ...

  5. 《机器学习实战》-逻辑(Logistic)回归

    目录 Logistic 回归 本章内容 回归算法 Logistic 回归的一般过程 Logistic的优缺点 基于 Logistic 回归和 Sigmoid 函数的分类 Sigmoid 函数 Logi ...

  6. 使用java Apache poi 根据word模板生成word报表

    项目开发过程中,客户提出一堆导出报表的需求,需要导出word格式,页眉还需要加上客户公司的logo,试了几种方案,最后选择了用 Apache poi 加上自定义标签的方式实现. 目前功能还比较简单,一 ...

  7. Oracle学习(七)游标

    一.简介 定义 实质上是数据集,类似数组一样,把查询的数据集存储在内存当中. 使用时可以通过游标指向其中一条记录,也可以通过循环游标达到循环数据集的目的. 游标的种类 显式游标: 使用之前必须得先声明 ...

  8. 三维视觉惯性SLAM的有效Schmidt-EKF

    三维视觉惯性SLAM的有效Schmidt-EKF An Efficient Schmidt-EKF for 3D Visual-Inertial SLAM 论文地址: http://openaccess ...

  9. HashMap封装的数据用循环快速添加进list中产生的数据集全部相同的问题

    一.问题概述 在一次使用SimpleAdapter时,Data需要使用传入一条数据(Image.Text),该数据条使用HashMap封装.在用HashMap封装的数据用循环快速添加进list中产生了 ...

随机推荐

  1. Google 嘘! 嘘!

    https://www.gufen.gq(无广告,原guso.ml,ggso.ga,guge.ga) https://c.aiguso.tk (无广告,体验良好) https://d.freedo.g ...

  2. leetcode Ch1-Search

    一. Binary Search int binarySearch(vector<int> &array, int target) { , hi = array.size() - ...

  3. linux 下MySQL的安装

    一.安装MySQL   1.下载源码包     从mysql官网上下载linux下的source包mysql-5.0.51b.tar.gz,注意是下载GNU tar格式的,不是rpm包.    2.解 ...

  4. 基础知识:Promise(整理)

    基础知识:Promise(整理) (来自牛客网)下面关于promise的说法中,错误的是(D) A. resolve和reject都是直接生成一个进入相应状态的promise对象,其参数就是进入相应状 ...

  5. archer 安装

    archer 项目地址: https://github.com/jly8866/archer 安装docker版本 Pull Docker docker pull hhyo/archer 启动服务do ...

  6. 查看SQL运行时间

    set @d=now(); select * from table; select timestampdiff(second,@d,now());

  7. GPS-Graph Processing System Graph Coloring算法分析 (三)

        HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158     Graph coloring is the problem of assignin ...

  8. leaflet 中文API

    LeafLet js 官网:http://leafletjs.com/index.html LeafLet js 官网demo: http://leafletjs.com/examples.html ...

  9. SpringBoot实战(三)之使用RestFul Web服务

    一.导入maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  10. Filters in ASP.NET Core (转自MSDN)

    Filters in ASP.NET Core MVC allow you to run code before or after specific stages in the request pro ...