Delphi Error – W1018 Case label outside of range of case expression

Delphi Compiler Error

W1018 Case label outside of range of case expression

Reason for the Error & Solution

You have provided a label inside a case statement which cannot be produced by the case statement control variable. -W

program Produce;
(*$WARNINGS ON*)

  type
    CompassPoints = (n, e, s, w, ne, se, sw, nw);
    FourPoints = n..w;

  var
    TatesCompass : FourPoints;

begin

   TatesCompass := e;
   case TatesCompass OF
   n:    Writeln('North');
   e:    Writeln('East');
   s:    Writeln('West');
   w:    Writeln('South');
   ne:   Writeln('Northeast');
   se:   Writeln('Southeast');
   sw:   Writeln('Southwest');
   nw:   Writeln('Northwest');
   end;
end.

It is not possible for a TatesCompass to hold all the values of the CompassPoints, and so several of the case labels will elicit errors.

program Solve;
(*$WARNINGS ON*)

  type
    CompassPoints = (n, e, s, w, ne, se, sw, nw);
    FourPoints = n..w;

  var
    TatesCompass : CompassPoints;

begin

   TatesCompass := e;
   case TatesCompass OF
   n:    Writeln('North');
   e:    Writeln('East');
   s:    Writeln('West');
   w:    Writeln('South');
   ne:   Writeln('Northeast');
   se:   Writeln('Southeast');
   sw:   Writeln('Southwest');
   nw:   Writeln('Northwest');
   end;
end.

After examining your code to determine what the intention was, there are two alternatives. The first is to change the type of the case statement’s control variable so that it can produce all the case labels. The second alternative would be to remove any case labels that cannot be produced by the control variable. The first alternative is shown in this example.

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...