【问题标题】:Delphi Set of EnumerationDelphi 枚举集
【发布时间】:2021-02-22 00:55:50
【问题描述】:

我正在尝试过滤我的日志记录。如果选项中有日志(或消息)类型,我想将其发送到日志,否则退出。

在“if not MessageType in...”行编译失败:

"[dcc32 Error] uMain.pas(2424): E2015 Operator not applicable to this operand type"

我认为基于 Include/Exclude 函数必须是可能的(并且相当简单),我尝试查看但在任何地方都找不到(即 Include(MySet, llInfo); )。

我的声明如下:

type
  TLogLevel = (llDebug, llError, llWarn, llInfo, llException);
  TLogOptions = set of TLogLevel;

var
  FLogOptions: TLogOptions; 

procedure TfrmMain.Log(const s: String; MessageType: TLogLevel;
  DebugLevel: Integer; Filter: Boolean = True);
begin


  if not MessageType in FLogOptions then
    exit;

  mmoLog.Lines.Add(s);
end;

【问题讨论】:

  • 抱歉!!我应该补充一点,在“if not MessageType in...”行编译失败:[dcc32 Error] uMain.pas(2424): E2015 Operator not applicable to this operand type
  • 复制那个。谢谢!

标签: delphi set enumeration


【解决方案1】:

由于运算符优先级,您需要在集合操作周围加上括号。编译器将其解析为if not MessageType,这不是一个有效的操作。如果在 set test 周围加上括号,编译器可以正确解析它。

if not (MessageType in FLogOptions) then

这是一个常见问题,并非特定于集合类型。例如,您也可以使用以下 express 得到相同的错误。

if not 1 = 2 and 2 = 3 then

在两个相等测试周围添加括号将更正错误。

if not (1 = 2) and (2 = 3) then

更多信息,您可以查看Operator Precedence的文档

【讨论】:

  • 这确实是一个非常常见的错误,尤其是= 案例。但在这种情况下,写1 <> 2 可能会更好。
  • 我希望 not 1 = 2 编译和评估为 False
  • 是的,“同样的错误”有点误导,因为 OP 谈到了 E2015“运算符不适用于此操作数类型”编译时错误。在此 A 中包含有关按位否定的简短警告可能是个好主意。
  • @DavidHeffernan:你是对的。我举了一个不好的例子。已编辑。
猜你喜欢
  • 2012-04-04
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多