Delphi Error – E2030 Duplicate case label

Delphi Compiler Error

E2030 Duplicate case label

Reason for the Error & Solution

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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...