如何检查枚举是否等于几个不同值中的任何一个?

Dan*_*dić 2 delphi enums

在该Data.DB单元中,声明了以下枚举:

TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord, ftBoolean, ftFloat, ...)
Run Code Online (Sandbox Code Playgroud)

我需要确定DataType搜索网格中的字段是否是整数、字符串、浮点数、日期、blob 等。

Integer 应该是任何可以用作整数的类型,例如ftSmallintftIntegerftWord等。

有没有比下面更短的方法来做到这一点?

if (Field.DataType = ftInteger) or (Field.DataType = ftSmallint) or (Field.DataType = ftWord) then Result := ftInteger;
Run Code Online (Sandbox Code Playgroud)

Mar*_*ynA 5

你可以这样做

if Field.DataType in [ftInteger, ftSmallInt, ftWord] then ...
Run Code Online (Sandbox Code Playgroud)

另外,您可以将集合类型定义为 aset of TFieldType并使用该类型的变量来存储您要查找的字段类型,然后使用if Field.DataType in ...它。

  • 我会使用常量而不是变量: `const IntegerFields = [ftInteger, ftSmallInt, ftWord]; ...如果 IntegerFields 中的 Field.DataType 那么 ...` (2认同)