2

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...

2
  • 1
    @David and GJ both gave you answers. I wan tto point out that when you ask questions, it's much better to give the complete actual error message than it is to say "i am getting delphi compile time error". Knowing exactly what error you're getting makes it much easier to help you get answers.
    – Ken White
    Jun 14, 2011 at 11:06
  • @Ken: Thanks...I'll take care for such things in future :)
    – Nains
    Jun 14, 2011 at 13:04

4 Answers 4

8

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;
0
3

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)
  ]);
3
  • @Thanks David, thats error gone now...however I'll prefer TOndrej soln for my case..
    – Nains
    Jun 14, 2011 at 13:12
  • @Nains TOndrej solution leaves you with a constant, but it's somewhat less readable. That's the trade off I guess. Incidentally, my answer follows a pattern I use very frequently and I wish the language would support it a bit better. Jun 14, 2011 at 13:24
  • You can at least get rid of the ValuesArray function with language support: coarrType1Properties:=TValuesArray.Create(Values('HARDWARE',True),Values('ID',700)); Jun 14, 2011 at 15:44
0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.