[英]integer to an enumerated type as var?
For example I have enum 例如我有枚举
const
MY_FIRST = 1;
MY_SECOND = 2;
MY_THIRD = 3;
type
TMyEnum = MY_FIRST .. MY_THIRD;
and I have function like 我有类似的功能
procedure DoSomething(var _type: TMyEnum);
where I should pass integer
as TMyEnum
. 我应该将integer
传递为TMyEnum
。 By doing 通过做
var
some_int_value: integer;
begin
DoSomething(TMyEnum(some_int_value));
I have error [DCC Error] : E2197 Constant object cannot be passed as var parameter
. 我有错误[DCC Error] : E2197 Constant object cannot be passed as var parameter
。 How to solve this? 如何解决呢?
A var
parameter requires a real variable, eg: var
参数需要一个实变量,例如:
var
some_int_value: integer;
my_enum: TMyEnum;
begin
my_enum := TMyEnum(some_int_value);
DoSomething(my_enum);
some_int_value := Ord(my_enum);
Since TEnum
is a subrange of Integer
the compiler only uses the smallest possible type that fits this subrange. 由于TEnum
是Integer
的子范围,因此编译器仅使用适合该子范围的最小类型。 In this case it is a Byte
. 在这种情况下,它是一个Byte
。
So if some_int_value
were declared as Byte
your typecast would succeed. 因此,如果将some_int_value
声明为Byte
您的类型转换将成功。 But it actually is declared as Integer
, so the typecast introduces a temporary Byte
to store the result of the typecast. 但是实际上它被声明为Integer
,因此类型转换引入了一个临时Byte
来存储类型转换的结果。 The compiler doesn't go that far to write any changes to this temprary Byte back to the source of the typecast. 编译器不会花太多时间将此临时字节的任何更改写回到类型转换的源。 That is why you get the described error. 这就是为什么您得到所描述的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.