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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With