P2006

Code

P2006

Message

Enumeration uses value that is not defined in the enumeration

This error occurs when an enumeration uses a value that is not defined in the enumeration.

Example

The following code will generate error P2006:

TYPE
    LogLevel : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
END_TYPE

FUNCTION_BLOCK Logger
  VAR_INPUT
    Trigger : BOOL;
    Message : STRING;
    Level : LogLevel := BLAH;  (* Error: BLAH is not defined in LogLevel *)
  END_VAR
END_FUNCTION_BLOCK

The variable Level is assigned the value BLAH, but BLAH is not one of the defined values in the LogLevel enumeration.

To fix this error, use a value that is defined in the enumeration:

TYPE
    LogLevel : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
END_TYPE

FUNCTION_BLOCK Logger
  VAR_INPUT
    Trigger : BOOL;
    Message : STRING;
    Level : LogLevel := INFO;  (* Correct: INFO is defined in LogLevel *)
  END_VAR
END_FUNCTION_BLOCK

The values of an enumeration are the values its own declaration lists. An enumeration declared as another enumeration’s name, such as Severity : LogLevel;, has the values of LogLevel. Two enumerations may list the same value name; each of them accepts it:

TYPE
    LogLevel : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
    Alarm : (CLEAR, WARNING, TRIPPED) := CLEAR;
END_TYPE

PROGRAM main
  VAR
    Level : LogLevel := WARNING;  (* Correct: WARNING is defined in LogLevel *)
    State : Alarm := WARNING;     (* Correct: WARNING is defined in Alarm *)
  END_VAR
END_PROGRAM

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.