使用`in'关键字导致“E1012常量表达式违反子范围边界”在德尔福 [英] Using the `in` keyword causes "E1012 Constant expression violates subrange bounds" in Delphi

查看:266
本文介绍了使用`in'关键字导致“E1012常量表达式违反子范围边界”在德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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常量表达式违反子范围

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
...

虽然我理解错误背后的原因,显示在,其中在整数数组的边界检查没有在编译时使用一个变量,似乎很奇怪,我得到相同

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?

推荐答案

有关 说:


基本类型不能超过256个可能的值,它们的序数必须在0到255之间。

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

因此,即使你可以有任何值的枚举,如果[a,b,c] 语句中的xx会失败,因为一个集合不能容纳大于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.

使用case语句:

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

end;

这篇关于使用`in'关键字导致“E1012常量表达式违反子范围边界”在德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆