有没有更简单的方法基于布尔值定义Enum类型? [英] Is there a easier way to define a Enum type based on a boolean value?

查看:108
本文介绍了有没有更简单的方法基于布尔值定义Enum类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不完全确定问题的措词是否恰当,但我之前曾问过与此问题相关的问题:如何正确地在类中将Set设置为属性?

Not entirely sure if the question is worded appropriately or not, but I previously asked this question which relates to this one: How do I correctly implement a Set in a class as a property?

我希望代码尽量简短,最少和可读,这就是我认为可以将某些代码编写得更好但我正在运行的地方

I like to keep code as short, minimal and readible as possible, and this is where I think some code could be written better but I am running into problems.

读取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;

更简洁,更短,更好的方式:

The cleaner, short and better way:

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






现在我要这样做另一种方法是设置值。

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

Currently the only way I know is the long way:

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];

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

If possible I would like to do something like this:

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 Include 成员,但我不确定这里是否需要这些成员。

There is the Exclude and Include members but I am unsure if these are going to be needed here or not.

因此如上所述-是否有更简单的方法基于布尔值定义Enum类型?

So, as described above - Is there a easier way to define a Enum type based on a boolean value?

谢谢。

推荐答案

不,对于此时在Delphi中键入 set 。我建议创建一个辅助索引属性,该属性可以模拟像访问这样的数组,并且将隐藏需要包含或排除集合中的元素的代码。

No, there is no array like access available for set type in Delphi at this time. I would suggest making a helper indexed property which can mimic array like access and which will hide a code needed to include or exclude an element from the set.

到目前为止,人们通常使用 Is Has 前缀为具有基本访问权限的属性添加前缀。在您的情况下,我将遵循此规则并创建一个名为 HasIDECompatibility 的属性,或者说一个 IsIDECompatible 的属性。此属性将为 Boolean 类型,并且将具有getter,通过该getter可以返回有关元素是否在内部set字段内的信息。在setter中,它将根据输入值将元素包括或排除在集合中。

From what I've seen so far, people usually prefix properties that has some elementary access by Is or Has prefixes. In your case I would follow this rule and make a property called HasIDECompatibility or let's say IsIDECompatible. This property will be of Boolean type and will have a getter, by which you return the information whether the element is inside the internal set field or not. In setter it will either include or exclude the element into the set depending on the input value.

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

In a more generalized code it would be:

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;

这篇关于有没有更简单的方法基于布尔值定义Enum类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆