Delphi – E2072 Number of elements (%d) differs from declaration (%d)

When you declare a typed constant or initialised variable of array type in your Delphi program but don’t specify the correct amount of items, this error message appears.

program Produce;

var
  A : array [1..10] of Integer = (1,2,3,4,5,6,7,8,9);

begin
end.

Although the sample declares a 10-element array, the initialization only provides 9 entries.

program Solve;

var
  A : array [1..10] of Integer = (1,2,3,4,5,6,7,8,9,10);

begin

To make the compiler happy, we just had to provide the missing piece. It can be difficult to tell if you’ve supplied the correct number of elements when initialising larger arrays. To aid in this, you can arrange the source file in such a way that counting is simple (for example, ten elements per line), or you can write the index of an element in comments adjacent to the element itself.

%d bloggers like this: