P4101

Code

P4101

Message

CASE label is not a value of the selector type

This error occurs when a CASE statement selects on an integer and one of its labels is a name, such as an enumerated value.

A CASE compares the selector with each label, so each label must be a value of the selector’s type. When the selector is an integer (INT, DINT, a subrange of an integer type, or an integer literal), its labels are integers and integer ranges. An enumerated value such as Fill is a value of its enumeration, not an integer. A name that nothing declares is not a value of any type.

Example

The following code will generate error P4101 twice:

TYPE
    Mode : (Idle, Fill);
END_TYPE

PROGRAM main
    VAR
        step : INT;
        alarm : BOOL;
    END_VAR
    CASE step OF
        Fill: alarm := TRUE;    (* Error: Fill is a Mode, not an INT *)
        Drain: alarm := FALSE;  (* Error: nothing declares Drain *)
    END_CASE;
END_PROGRAM

step is an INT, so the labels of a CASE that selects on it are integers. Fill is a value of Mode, and Drain is not declared.

To fix this error, label an integer selector with integers. Where the labels are the values of an enumeration, select on a variable of that enumeration:

TYPE
    Mode : (Idle, Fill);
END_TYPE

PROGRAM main
    VAR
        step : INT;
        mode : Mode;
        alarm : BOOL;
    END_VAR
    CASE step OF
        1: alarm := TRUE;
        2: alarm := FALSE;
    END_CASE;
    CASE mode OF
        Fill: alarm := TRUE;
        Idle: alarm := FALSE;
    END_CASE;
END_PROGRAM

See Also

  • CASE — the CASE statement

  • P2006 — a label of a CASE on an enumeration is not one of its values

  • P2045 — a label is written with an enumeration other than the selector’s type

  • P4053 — the selector is not an integer or an enumeration

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.