Delphi 2007变种类型初始化

Nai*_*ins 1 delphi 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)  
 );  
Run Code Online (Sandbox Code Playgroud)

但我得到类型值的delphi编译时错误,即此类型无法初始化.如何防止此错误?或者我们可以有替代解决方案等.请协助......

Ond*_*lle 7

对于这些(布尔,整数)和其他简单类型,您可以使用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;
Run Code Online (Sandbox Code Playgroud)