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
成员,但我不确定这里是否需要这些。
那么,如上所述 - 是否有更简单的方法来定义基于布尔值的枚举类型?
谢谢。