Delphi – E2030 Duplicate case label

When there are many case labels in your delphi program with the same value in a case statement, this error notice appears.

program Produce;

function DigitCount(I: Integer): Integer;
begin
  case Abs(I) of
  0                     DigitCount := 1;
  0        ..9         DigitCount := 1;   (*<-- Error message here*)
  10       ..99        DigitCount := 2;
  100      ..999       DigitCount := 3;
  1000     ..9999      DigitCount := 4;
  10000    ..99999     DigitCount := 5;
  100000   ..999999    DigitCount := 6;
  1000000  ..9999999   DigitCount := 7;
  10000000 ..99999999  DigitCount := 8;
  100000000..999999999 DigitCount := 9;
  else                  DigitCount := 10;
  end;
end;

begin
  Writeln( DigitCount(12345) );
end.

We didn’t pay attention here, and the case label 0 was specified twice.

How to fix it?

program Solve;

function DigitCount(I: Integer): Integer;
begin
  case Abs(I) of
  0        ..9         DigitCount := 1;
  10       ..99        DigitCount := 2;
  100      ..999       DigitCount := 3;
  1000     ..9999      DigitCount := 4;
  10000    ..99999     DigitCount := 5;
  100000   ..999999    DigitCount := 6;
  1000000  ..9999999   DigitCount := 7;
  10000000 ..99999999  DigitCount := 8;
  100000000..999999999 DigitCount := 9;
  else                  DigitCount := 10;
  end;
end;

begin
  Writeln( DigitCount(12345) );
end.

When you have symbolic constants and ranges of case labels, the problem may not be obvious; you may need to write down the true values of the constants to figure out what’s wrong.