Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record in record (Cannot initialize)

Tags:

delphi

I have 2 records like this:

TYPE
 TRecord2= packed record
  i2: Integer;
 end;

 TRecord1= packed record
  i1: Integer;
  R2: TRecord2;
 end;

.

I want to initialize the record fields to zero but I don't want to use FillMemory so I declared 2 constant records in which I initialize the fields.

CONST
  Record2c: TRecord2=
  (
   i2: 0;
  );

  Record1c: TRecord1=
  (
    i1: 0;
    R2: Record2c;      <------- error line
  );

However, I cannot assign a Record2c to R2 field. The compiler says: E2029 '(' expected but identifier 'Record2c' found.

But this works (if I comment the line where I have the error):

procedure test;
var Record1: TRecord1;
begin
 Record1:= Record1c;      // initialize variable by associating the constant to it
end

So, how do i initialize the R2 field?

like image 688
Server Overflow Avatar asked Dec 12 '22 11:12

Server Overflow


1 Answers

You can only initialize consts with true constants. True constants do not have types — those are typed constants. See Declared Constants in the Delphi documentation. Record2c in your code is a typed constant, so it cannot be used in const expressions like the one required for initializing Record1c. You'll just have to in-line the definition of Record1c.R2:

const
  Record1c: TRecord1 = (
    i1: 0;
    R2: (i2: 0;);
  );

When you comment out the error line, you're leaving the R2 field default-initialized to zeros.

like image 149
Rob Kennedy Avatar answered Dec 28 '22 08:12

Rob Kennedy