有没有更简单的方法来定义基于布尔值的枚举类型?

2024-02-16

Overview

不完全确定这个问题的措辞是否恰当,但我之前问过与此相关的问题:如何在类中正确实现 Set 作为属性? https://stackoverflow.com/questions/24579145/how-do-i-correctly-implement-a-set-in-a-class-as-a-property

我喜欢让代码尽可能简短、最小化和可读,这就是我认为一些代码可以写得更好的地方,但我遇到了问题。


读取 Set 中的值的两种方法中的第一种示例:

漫长的路:

if (Delphi1 in IDECompatibility) then
  CheckListBox1.Checked[0] := True;
if (Delphi2 in IDECompatibility) then
  CheckListBox1.Checked[1] := True;
if (Delphi3 in IDECompatibility) then
  CheckListBox1.Checked[2] := True;

更干净、更短、更好的方法:

CheckListBox1.Checked[0] := (Delphi1 in IDECompatibility);
CheckListBox1.Checked[1] := (Delphi2 in IDECompatibility);
CheckListBox1.Checked[2] := (Delphi3 in IDECompatibility);

现在我想用另一种方式来设置值。

目前我知道的唯一方法是漫长的道路:

if CheckListBox1.Checked[0] then
  IDECompatibility := IDECompatibility + [Delphi1]
else
  IDECompatibility := IDECompatibility - [Delphi1];

if CheckListBox1.Checked[1] then
  IDECompatibility := IDECompatibility + [Delphi2]
else
  IDECompatibility := IDECompatibility - [Delphi2];

if CheckListBox1.Checked[2] then
  IDECompatibility := IDECompatibility + [Delphi3]
else
  IDECompatibility := IDECompatibility - [Delphi3];

如果可能的话我想做这样的事情:

IDECompatibility[Delphi1] := CheckListBox1.Checked[0]; // Array type required
IDECompatibility[Delphi2] := CheckListBox1.Checked[1]; // Array type required
IDECompatibility[Delphi3] := CheckListBox1.Checked[2]; // Array type required

有的是Exclude and Include成员,但我不确定这里是否需要这些。

那么,如上所述 - 是否有更简单的方法来定义基于布尔值的枚举类型?

谢谢。


不,没有类似数组的访问可用set http://docwiki.embarcadero.com/RADStudio/XE6/en/Structured_Types#Sets此时输入Delphi。我建议创建一个辅助索引属性,它可以模拟类似数组的访问,并且隐藏包含或排除集合中的元素所需的代码。

从我到目前为止所看到的来看,人们通常会为具有一些基本访问权限的属性添加前缀Is or Has前缀。在你的情况下,我会遵循这条规则并创建一个名为HasIDECompatibility或者说IsIDECompatible。该房产将属于Booleantype 并有一个 getter,通过它可以返回元素是否在内部 set 字段内的信息。在 setter 中,它将根据输入值将元素包含或排除到集合中。

在更通用的代码中,它将是:

type
  TElement = (
    Element1,
    Element2,
    Element3
  );
  TElements = set of TElement;

  TMyClass = class
  private
    FElements: TElements;
    function GetElement(Kind: TElement): Boolean;
    procedure SetElement(Kind: TElement; Value: Boolean);
  public
    // this property is for direct access to the set field; if your class would
    // be a TComponent descendant and you'd publish this property, you'd see it
    // in the Object Inspector represented by a set of check boxes
    property Elements: TElements read FElements write FElements;
    // this property is just a helper for array like access to the internal set
    // field
    property HasElement[Kind: TElement]: Boolean read GetElement write SetElement;
  end;

implementation

{ TMyClass }

function TMyClass.GetElement(Kind: TElement): Boolean;
begin
  Result := Kind in FElements;
end;

procedure TMyClass.SetElement(Kind: TElement; Value: Boolean);
begin
  if Value then
    Include(FElements, Kind)
  else
    Exclude(FElements, Kind);
end;

以及一个示例用法:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyClass: TMyClass;
begin
  MyClass := TMyClass.Create;
  try
    // write elementary property value
    MyClass.HasElement[Element1] := CheckBox1.Checked;
    MyClass.HasElement[Element2] := CheckBox2.Checked;
    // read elementary property value
    CheckBox3.Checked := MyClass.HasElement[Element1];
    CheckBox4.Checked := MyClass.HasElement[Element2];

    // or you can access MyClass.Elements set at once as you were used to do
    // which gives you two ways to include or exclude elements to the set
  finally
    MyClass.Free;
  end;
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有更简单的方法来定义基于布尔值的枚举类型? 的相关文章

  • 如何安装DBMonitor

    这可能是一个非常简单的问题 但就是这样 我刚刚更新了 Firebird 的 DevArt DBExpress 驱动程序的许可证 帮助文件说我可以使用他们的免费软件 DBMonitor 应用程序 但由于我使用的是 D2006 所以我必须使用以
  • 是否有适用于 >= Delphi 2007 的 Delphi 混淆器

    我曾经使用 Pythia 来混淆我的 D6 程序 但 Pythia 似乎不再适用于我的 D2007 这是 Pythia 的链接 自 2007 年初以来没有更新 http www the interweb com serendipity in
  • 无法通过Delphi替换Word应用程序中的页脚文本

    我正在尝试替换页脚中的文本 我使用以下代码 通过它我可以替换正常内容 正文 但不能替换页脚 页眉中的内容 myWinWordApp CreateOLEObject Word Application myWinWordApp visible
  • Delphi - 通过 ADO 查询获取 Excel 行

    我有以下 Excel 文件 我将 AdoConnection ConnectionString 设置为 AdoConnection ConnectionString Provider Microsoft Jet OLEDB 4 0 Data
  • 如何更改 Delphi 2010 IDE 中编辑器选项卡的字体大小?

    有谁知道如何更改 Delphi 2010 IDE 中编辑器选项卡的字体大小 我的 1080p 22 显示器的字体太小 无法阅读 而且会导致眼睛疼痛 一些笔记 它不尊重系统的 DPI 设置 因此更改系统设置没有帮助 而且 我现在已经使用 14
  • Delphi (Indy) TIdTCPClient 在线程中

    在互联网上 我看到通常将 TIdTCPClient 放置在自定义 TThread 后代中 为什么要这样做 有时我也在这样的线程中看到服务器 为什么 干杯 阿德里安 Indy 使用阻塞 I O 最好在线程中处理 这是 Indy 整体设计的核心
  • 使用Delphi检查HTML代码中是否有对象属性值

    如何使用Delphi检查HTML代码中是否有输入对象属性值 there isn t value attribute
  • 开源 Delphi 包可使用哪些项目选项?

    我写了一些 Delphi 代码 想在 GitHub 上分享 所有代码都根据需要包含在运行时和设计时包中 每个项目有许多项目选项需要设置 输出目录 搜索路径 编译选项等 我设法找到了一些适合我的情况的默认选项 但阅读此处的其他问答很明显有多个
  • 如何将 REST API 与 FireMonkey 结合使用?

    我需要在 FireMonkey 中实现 REST API 来获取一些信息 但我不确定如何做到这一点 REST API使用OAuth2 我可以访问两个代码 Consumer Key和Consumer Secret 之后 我需要获得一个临时的
  • 德尔福数据结构

    我可能需要在 Delphi 中做一个项目 并且是该领域的初学者 目前 我正在网上搜索资源 但由于资源站点太少而感到困惑 首先 你能给我一些好的网站 其中包含我迄今为止错过的 Delphi 资源吗 我也在 Delphi 中搜索数据结构 想知道
  • 如何仅使用 TADOQuery 组件将图像插入数据库

    我有一个简单的基本问题 我正在尝试使用将图像插入数据库Insert与其他列值的语句也使用TADOQuery成分 由于代码已经由某人编写 因此我想在此处放置一些虚拟示例代码 以供您澄清相应的步骤 请注意 这可以正常工作TQuery组件 因为我
  • 我如何在Delphi中处理事件?

    例如 我有一个程序 在单击 Button1 后执行某些操作 如果没有 Button1Click 中的代码 如何处理按钮的 onclick 事件 我需要为 Button1 动态添加事件 unit Unit1 interface uses Wi
  • TDictionary 上的 GetItem 由链接器消除

    我正在使用一个TDictionary of
  • 处理 TShellListView 后代中的文件放置

    我正在尝试创建 TShellListView 的后代 它接受从 Windows 资源管理器中删除的文件 我想在组件定义中处理拖 放操作 而不必在任何使用该组件的应用程序中实现它 我找到了接受从 Windows 资源管理器中拖放的文件的示例
  • 如何在调试器中显示 TStringList 的内容?

    我想在调试应用程序时显示 TStringList 的全部内容 相反 我只是得到指示 Flist 仅显示地址 如果您使用的是 Delphi 2010 或更高版本 调试器允许使用调试可视化工具 http docwiki embarcadero
  • Word 2010 自动化:“转到书签”

    我有一个用 Delphi 7 编写的程序 它打开一个基于模板的新 Word 文档 文档打开后 系统会自动跳转到书签 在模板中预定义 并在其中添加一些文本 以下代码在 Word 2003 中工作正常 但会导致invalid variant o
  • 在 Delphi 中使用 XML(将特定数据返回到变量)

    过去几天我一直在尝试使用 Delphi 2010 和 MSXML 我是一个极端的新手 需要一点指导 var MemoryStream TMemoryStream XMLPath String sName String XMLDoc vari
  • 石和磅的格式正确吗?

    我有一个图表 用于显示重量 以英石和磅 lbs 为单位 该图表由记录中的数据填充 对于权重 数据类型为 Double 记录数据是在运行时编辑的 我需要知道一种正确格式化输入数据的方法 为了更好地理解 首先看一下这些示例值 它们表示为石和磅
  • delphi中如何实现多重继承?

    我正在对一个旧库进行完全重写 我不确定如何处理这种情况 为了便于理解 大家都欢呼自行车类比 我有以下课程 TBike 自行车本身 TBikeWheel 自行车的一个轮子 TBikeWheelFront and TBikeWheelBack
  • 如何更改 TPageControl 上标签的方向?

    我是 Delphi 的新手 再次强调 我在 1994 年就使用过 Delphi 我现在有 Delphi 2009 Pro 来自Java 我发现对象继承非常晦涩 我的用户想要选项卡位于左侧的选项卡式页面 但是 TPageControl 不允许

随机推荐