-

Enumerations (enums)

This topic contains the following sections:

Overview

An enumeration (enum) is a user-defined data type that consists of a comma-separated list of given values called enumeration values or enumerators. The variable of an enum data type can take only one value chosen from the list of enumerators. Other variable values are not possible.

The enumerators are either literals or local or global constants defined in the project. (Local constant = constant is declared in the data type worksheet by a VAR CONSTANT...END_VAR declaration block. Global constant = the 'Constant' attribute is selected for the variable in corresponding variables table or data list.)

Enumerated data types vs data types with named values

According to the IEC 61131-3 standard, PLCnext Engineer supports enumerated data types and data types with named values (both called enumerations in this help). An enumerated data type contains a list of enumerators where there are no values assigned to the enumerators (values of the enumerators are not known). In contrast to this, the enumerators of a data type with named values have a fixed value assigned (see also the section Initialization below).

Enumerated data typeTYPE
   PossiblColors : (Red, Green, Blue);
END_TYPE
Data type with named valuesTYPE
   PossiblColors : (Red := 2, Green := 5, Blue := 10);
END_TYPE

Type definition

An enumeration data type is defined in the data type worksheet using the TYPE ... END_TYPE declaration keywords. The enumerators are declared inside parentheses () and separated by a comma. In the 'Variables' table or Data List, you can declare a variable of the enumeration type. This variable can only take one of the enumerators specified for the enum data type.

In the following example, the enumeration 'PossibleColors' contains the enumerators Red, Green, and Blue. The 'Color' variable of type enum can only take one of the enumerators Red, Green, and Blue. The ST code example assigns the value Green to the 'Color' variable if 'Start' = TRUE.

Data type definition
Variable declaration
('Variables'/Data List)
Use in code

Note
If two or more enumerations contain an enumerator with the same name, the ENUM name can optionally be added before the element name, followed by a hash character.
Example: two enumeration contain the enumerator Green.
TYPE
   EnumColor : (Red := 0, Green := 10, Blue := 20) OF INT := Green;
   EnumBackgound : (Red := 255, Green := 128, Blue := 0) OF INT := Green;
END_TYPE
In the code, the statement eColor := EnumBackgound#Green; is unique.

Initialization

Each enumerator has a name and an underlying base type (integer by default; see also the section Enumerations with explicit base data types below). By default, the enumerator listed first has the default initialization value 0, and the value of each successive enumerator is increased by 1. In the example above, Red has the value 0, Green the value 1, and Blue the value 2.

Initialize single enumerators

You can explicitly replace the default initialization values by user-defined values using the assignment operator ":=" as shown in the following example. In this declaration, the first enumerator Red has the initialization value 2, Green has the value 3 (successive values without initialization value will automatically increase by 1) and Blue has the value 10.

The initialization value can be either a literal (fixed value) or a local or global constant.

Example:
TYPE
   PossiblColors : (Red := 2, Green, Blue := 10);
END_TYPE

Specify a default value

If no other default value is explicitly specified, the enumerator listed first is used as default value for the initialization of the enumeration. You can override this default value by a user-defined value using the assignment operator ":=" as shown in the following example. The default value must be an enumerator of the given values. In the following example, the variable PossibleColors of type enum is initialized with the value Green.

TYPE
   PossiblColors : (Red, Green, Blue) := Green;
END_TYPE

Variable declaration with variable of type enum and configured initialization value ('Init' column):

Note
You can use the 'Init Value Configuration' editor to specify the default value of the enumeration.

Enumerations with explicit base data types

Each enumerator has a base data type of ANY_INT. The base type determines the data type of the enumerators. By default, this is INT. You can explicitly define another base type for the enumeration as shown in the following example.

Example with explicitly defined base type:
TYPE
   PossiblColors : (Red := 16#FFFFFFF0 , Green := 16#FFF0000F, Blue ;= 16#55000000) OF DINT;
END_TYPE