开发者

Delphi 2007 variant type initialization

开发者 https://www.devze.com 2023-03-12 13:43 出处:网络
I am trying to declare a constant array for validating type properties held by input object. but i am doing something incorrect, please have 开发者_StackOverflowa look at below code:

I am trying to declare a constant array for validating type properties held by input object. but i am doing something incorrect, please have 开发者_StackOverflowa 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...


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;


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)
  ]);


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号