I need to initialize an array in a Delphi Initialization
block.
It appears that you cannot use a var
block in an initialize block because this won’t compile:
initialization
var
idx : Integer;
begin
for idx := 0 to length(LastState)-1 do begin
LastState[idx] := $FFFF;
end;
end;
(The first compilation error complains about var
):
([DCC Error] ScheAutoInfRb2.pas(6898): E2029 Statement expected but 'VAR' found)
This does not compile either (because idx
is not declared):
initialization
for idx := 0 to length(Last_Pro2State)-1 do begin
Last_Pro2State[idx] := $FFFF;
end;
[DCC Error] ScheAutoInfRb2.pas(6899): E2003 Undeclared identifier: 'idx'
I know that I can declare an indexer in the main unit declaration, but that has a couple of disadvantages:
implementation
section (which can be hundreds of lines away), andImplementation
section.You can't.
The usual way to do this is to write a procedure that you then call from the initialization
section:
procedure InitLastStateArray;
var
idx : Integer;
begin
for idx := 0 to length(LastState)-1 do begin
LastState[idx] := $FFFF;
end;
end;
initialization
IntLastStateArray;
end.