使用`in`关键字导致“E1012常数表达式违反子范围”在德尔福 [英] Using the `in` keyword causes "E1012 Constant expression violates subrange bounds" in Delphi
问题描述
来检查项目是否在常量数组中时,我收到以下编译错误:
E1012常数表达式违反子范围
常量定义如下:
类型TSomeEnum =(seFoo = 1000,
seBar = 2000,
seBoo = 3000,
seFar =
seFooBar = 5000,
seBooFar = 6000,
seLow = 1000,
seHigh = 6000,
seCount = 6);
失败的行如下:
如果[seFoo,seFar,seFooBar]中的someObj.someProperty然后
...
虽然我了解错误背后的原因在另一个问题发布在这里,在使用变量的编译时没有完成对整数数组进行检查的边界,看起来很奇怪,我得到相同一个常数数组的问题,这绝对是在边界内。
现在,我用一个(大得多)的语句替换了这个行,它包含或
子句。但是这显然不是很理想。任何人都可以看出为什么我得到这个问题?
解决方案有关 设置
说:
基类型可以不超过256个可能的值,其序数必须在0到255之间。
所以即使你可以有任何值的枚举,如果[a,b,c]
语句中的xx将在这里失败,那么一个大于255的值。
改用case语句:
case xx
a,b,c:// make something
end;
I've come across some rather unusual behaviour in a bit of Delphi code. When using the in
keyword to check if an item is in a constant array, I get the following compilation error:
E1012 Constant expression violates subrange bounds
The constants are defined as follows:
type TSomeEnum = (seFoo = 1000,
seBar = 2000,
seBoo = 3000,
seFar = 4000,
seFooBar = 5000,
seBooFar = 6000,
seLow = 1000,
seHigh = 6000,
seCount = 6);
The line that is failing is the following:
if someObj.someProperty in [seFoo, seFar, seFooBar] then
...
Whilst I understand the reasoning behind the error showing in another question posted here, where bounds checking on integer arrays wasn't done at compile time when using a variable, it seems odd that I'm getting the same problem with a constant array which is most certainly within bounds.
For now, I've replaced the line with a (much larger) statement comprising of or
clauses. However, this is clearly not ideal. Can anyone shed any light on why I'm getting this problem?
解决方案 Documentation about Sets
says :
The base type can have no more than 256 possible values, and their ordinalities must fall between 0 and 255.
So even if you can have enums of any value, the if xx in [a,b,c]
statement will fail here, since a set cannot hold a value larger than 255.
Use a case statement instead:
case xx of
a,b,c : // Make something
end;
这篇关于使用`in`关键字导致“E1012常数表达式违反子范围”在德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!