Delphi 2007变体类型初始化

发布于 2024-11-15 05:36:39 字数 568 浏览 4 评论 0原文

我试图声明一个常量数组来验证输入对象持有的类型属性。但我做了一些不正确的事情,请看一下下面的代码:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送舟行 2024-11-22 05:36:39

对于这些(布尔、整数)和其他简单类型,您可以使用 TVarData 进行初始化,并将类型转换回 Variant

type
  TValues = record
    Name: WideString;
    Value: TVarData;
  end;

const
  coarrType1Properties : array[0..5] of TValues = (
    (Name: 'HARDWARE'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'SOFTWARE'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'TAG'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'AUTHORIZED'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'ID'; Value: (VType: varInteger; VInteger: 700)),
    (Name: 'CODE'; Value: (VType: varInteger; VInteger: 0))
  );

procedure Test;
var
  I: Integer;
begin
  for I := Low(coarrType1Properties) to High(coarrType1Properties) do
    Writeln(Format('coarrType1Properties[%d]: ''%s'', %s', [I, coarrType1Properties[I].Name, VarToStr(Variant(coarrType1Properties[I].Value))]));
end;

For these (Boolean, Integer) and other simple types, you could initialize with TVarData and typecast back to Variant:

type
  TValues = record
    Name: WideString;
    Value: TVarData;
  end;

const
  coarrType1Properties : array[0..5] of TValues = (
    (Name: 'HARDWARE'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'SOFTWARE'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'TAG'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'AUTHORIZED'; Value: (VType: varBoolean; VBoolean: True)),
    (Name: 'ID'; Value: (VType: varInteger; VInteger: 700)),
    (Name: 'CODE'; Value: (VType: varInteger; VInteger: 0))
  );

procedure Test;
var
  I: Integer;
begin
  for I := Low(coarrType1Properties) to High(coarrType1Properties) do
    Writeln(Format('coarrType1Properties[%d]: ''%s'', %s', [I, coarrType1Properties[I].Name, VarToStr(Variant(coarrType1Properties[I].Value))]));
end;
贱贱哒 2024-11-22 05:36:39

文档指出:

文件类型(包括Text类型)和Variant类型无法初始化,即不能声明这些类型的类型常量或初始化变量。

所以你的问题在于你的变体记录成员。这意味着您需要不同的方法,并且必须放弃使用常量数组。

function Values(const Name: WideString; const Value: Variant): TValues;
begin
  Result.Name := Name;
  Result.Value := Value;
end;

type
  TValuesArray = array of TValues;

function ValuesArray(const Values: array of TValues): TValuesArray;
var
  i: Integer;    
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

var
  coarrType1Properties: TValuesArray;

initialization
  coarrType1Properties := ValuesArray([
    Values('HARDWARE', TRUE),
    Values('SOFTWARE', TRUE),
    Values('TAG', TRUE),
    Values('AUTHORIZED', TRUE),
    Values('ID', 700),
    Values('CODE', 0)
  ]);

The documentation states:

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.

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.

function Values(const Name: WideString; const Value: Variant): TValues;
begin
  Result.Name := Name;
  Result.Value := Value;
end;

type
  TValuesArray = array of TValues;

function ValuesArray(const Values: array of TValues): TValuesArray;
var
  i: Integer;    
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

var
  coarrType1Properties: TValuesArray;

initialization
  coarrType1Properties := ValuesArray([
    Values('HARDWARE', TRUE),
    Values('SOFTWARE', TRUE),
    Values('TAG', TRUE),
    Values('AUTHORIZED', TRUE),
    Values('ID', 700),
    Values('CODE', 0)
  ]);
墟烟 2024-11-22 05:36:39

E2071:无法使用常量表达式初始化变体。

E2071: Variants can not be initialized with a constant expression.

£烟消云散 2024-11-22 05:36:39

D2007 表格帮助:
E2071:该类型无法初始化

File类型(包括类型Text),并且类型Variant无法初始化,即不能声明这些类型的类型常量或初始化变量。

program Produce;

var
  V: Variant = 0;

begin
end.

// 该示例尝试声明一个 Variant 类型的已初始化变量,这是非法的。

program Solve;

var
  V: Variant;

begin
  V := 0;
end.

解决方案是使用赋值语句初始化普通变量。

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.

program Produce;

var
  V: Variant = 0;

begin
end.

// The example tries to declare an initialized variable of type Variant, which illegal.

program Solve;

var
  V: Variant;

begin
  V := 0;
end.

The solution is to initialize a normal variable with an assignment statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文