相关疑难解决方法(0)

如何比较枚举类型的集合

从某一点来说,我厌倦了编写设置条件(and,or),因为对于更多条件或更长的变量名称,它开始变得笨拙并且令人讨厌再次写入.所以我开始写助手,所以我可以写ASet.ContainsOne([ceValue1, ceValue2])而不是(ceValue1 in ASet) or (ceValue2 in ASet).

type
  TCustomEnum = (ceValue1, ceValue2, ceValue3);
  TCustomSet = set of TCustomEnum;
  TCustomSetHelper = record helper for TCustomSet 
    function ContainsOne(ASet: TCustomSet): Boolean;
    function ContainsAll(ASet: TCustomSet): Boolean;
  end;

implementation

function TCustomSetHelper.ContainsOne(ASet: TCustomSet): Boolean;
var
  lValue : TCustomEnum;
begin
  for lValue in ASet do
  begin
    if lValue in Self then
      Exit(True);
  end;
  Result := False;
end;

function TCustomSetHelper.ContainsAll(ASet: TCustomSet): Boolean;
var
  lValue : TCustomEnum;
begin
  Result := …
Run Code Online (Sandbox Code Playgroud)

delphi delphi-10.2-tokyo

8
推荐指数
2
解决办法
249
查看次数

标签 统计

delphi ×1

delphi-10.2-tokyo ×1