E2030 Duplicate case label (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

This error message occurs when there is more than one case label with a given value in a case statement.


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.

Here we did not pay attention and mentioned the case label 0 twice.


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.

In general, the problem might not be so easy to spot when you have symbolic constants and ranges of case labels - you might have to write down the real values of the constants to find out what is wrong.