I need to initialize and 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:
The declaration of the indexer is separated from its use but the implementation section (which can be hundreds of lines away), and
The scope of the indexer includes all the functions and procedures in the Implementation 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.