Delphi 2007变体类型初始化
我试图声明一个常量数组来验证输入对象持有的类型属性。但我做了一些不正确的事情,请看一下下面的代码:
// Record to hold Name-Value pair for checking entities
TValues = record
Name : WideString;
Value : Variant;
end;
const
coarrType1Properties : array[0..5] of TValues =
(
(Name : 'HARDWARE'; Value : TRUE),
(Name : 'SOFTWARE'; Value : TRUE),
(Name : 'TAG'; Value : TRUE),
(Name : 'AUTHORIZED'; Value : TRUE),
(Name : 'ID'; Value : 700),
(Name : 'CODE'; Value : 0)
);
但我收到类型值的delphi编译时错误,即此类型无法初始化。如何防止这个错误呢?或者我们可以有替代解决方案等吗?请协助...
I am trying to declare a constant array for validating type properties held by input object. but i am doing something incorrect, please have a look at below code:
// Record to hold Name-Value pair for checking entities
TValues = record
Name : WideString;
Value : Variant;
end;
const
coarrType1Properties : array[0..5] of TValues =
(
(Name : 'HARDWARE'; Value : TRUE),
(Name : 'SOFTWARE'; Value : TRUE),
(Name : 'TAG'; Value : TRUE),
(Name : 'AUTHORIZED'; Value : TRUE),
(Name : 'ID'; Value : 700),
(Name : 'CODE'; Value : 0)
);
but I am getting delphi compile time error for type value i.e. This type cannot be initialized. How to prevent this error? Or can we have alternate solution etc. Please assist...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这些(布尔、整数)和其他简单类型,您可以使用
TVarData
进行初始化,并将类型转换回Variant
:For these (Boolean, Integer) and other simple types, you could initialize with
TVarData
and typecast back toVariant
:文档指出:
所以你的问题在于你的变体记录成员。这意味着您需要不同的方法,并且必须放弃使用常量数组。
The documentation states:
So your problem is with your variant record member. This means that you need a different approach and you will have to abandon the use of a constant array.
E2071:无法使用常量表达式初始化变体。
E2071: Variants can not be initialized with a constant expression.
D2007 表格帮助:
E2071:该类型无法初始化
File类型(包括类型Text),并且类型Variant无法初始化,即不能声明这些类型的类型常量或初始化变量。
// 该示例尝试声明一个 Variant 类型的已初始化变量,这是非法的。
解决方案是使用赋值语句初始化普通变量。
Form D2007 help:
E2071: This type cannot be initialized
File types (including type Text), and the type Variant cannot be initialized, that is, you cannot declare typed constants or initialized variables of these types.
// The example tries to declare an initialized variable of type Variant, which illegal.
The solution is to initialize a normal variable with an assignment statement.