gpt4 book ai didi

delphi - 使用 `in` 关键字会导致 Delphi 中出现 "E1012 Constant expression violates subrange bounds"

转载 作者:行者123 更新时间:2023-12-03 14:37:31 26 4
gpt4 key购买 nike

我在一些 Delphi 代码中遇到了一些相当不寻常的行为。当使用 in 关键字检查某个项目是否在常量数组中时,出现以下编译错误:

E1012 Constant expression violates subrange bounds

常量定义如下:

type TSomeEnum = (seFoo = 1000,
seBar = 2000,
seBoo = 3000,
seFar = 4000,
seFooBar = 5000,
seBooFar = 6000,
seLow = 1000,
seHigh = 6000,
seCount = 6);

失败的行如下:

if someObj.someProperty in [seFoo, seFar, seFooBar] then
...

虽然我了解 another question posted here 中显示的错误背后的原因,当使用变量时,在编译时没有对整数数组进行边界检查,这似乎很奇怪,我在常量数组上遇到了同样的问题,而常量数组肯定在边界内。

目前,我已将该行替换为由 or 子句组成的(更大的)语句。然而,这显然并不理想。谁能解释一下为什么我会遇到这个问题?

最佳答案

关于 Sets 的文档说:

The base type can have no more than 256 possible values, and their ordinalities must fall between 0 and 255.

因此,即使您可以拥有任何值的枚举,if xx in [a,b,c] 语句也会在这里失败,因为集合不能容纳大于 255 的值。

改用 case 语句:

case xx of
a,b,c : // Make something

end;

关于delphi - 使用 `in` 关键字会导致 Delphi 中出现 "E1012 Constant expression violates subrange bounds",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12181617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com