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.