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 E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...