在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. [翻译] VBPieChart

    VBPieChart https://github.com/sakrist/VBPieChart Pie Chart iOS control with different animations to ...

  2. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  3. 错误:该表单无法显示,可能是由于 Microsoft SharePoint Server State Service 配置不当。有关详细信息,请与服务器管理员联系

    问题场景: 1.SharePoint 2013 中工作流需要状态服务(State Service),如果没有正确配置状态服务,则在给列表.文档库添加工作流时会遇到错误: “该表单无法显示,可能是由于 ...

  4. August 11th 2017 Week 32nd Friday

    I can't give you the world, but I can give you my world. 我不能给你全世界,但是我的世界我可以全部给你. Maybe I can't give ...

  5. Oracle 11g简体中文版的安装过程及图解

    1.下载Oracle 11g 地址 http://download.oracle.com/otn/nt/oracle11g/win32_11gR1_database_1013.zip 基本安装 解压缩 ...

  6. 2、Node.js 第一个应用

    内容:三种变量申明方式,Node.js应用组成,第一个应用创建+代码 ################################################################# ...

  7. python选课系统

    程序名称: 选课系统 角色:学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海 ...

  8. Angular2.0知识架构图

    知识架构图:

  9. VMware Harbor 学习

    Harbor简介 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distributio ...

  10. Hadoop学习之路(二十七)MapReduce的API使用(四)

    第一题 下面是三种商品的销售数据 要求:根据以上数据,用 MapReduce 统计出如下数据: 1.每种商品的销售总金额,并降序排序 2.每种商品销售额最多的三周 第二题:MapReduce 题 现有 ...