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