Enumerations (enums)
A variable of a user-defined enum data type can have a value selected from a predefined list. Other variable values are not possible.
Example: the enumeration 'PossibleColors' includes the values Red, Green, and Blue, each of which is an INTeger data type, i.e., Red = 0, Green = 1, and Blue = 2.
In ST, the 'Color' variable which is of the enum data type, is set to INT = 1, i.e., Green if Start = TRUE.
Data type definition | |
Variable declaration ('Variables'/Data List) | ![]() |
Use in code | ![]() |
Note
If two or more enumerations contain an element 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 element 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. |
Initializing enumerations
Enumerations can be initialized in two ways:- Values can be assigned to each element specified in the name lists (Blue := 5 in the example below). Without initializing these list elements, they are numbered continuously starting at 0 for the first element.
- One list element can be preselected either in the data type worksheet or the variables table as shown in the following example (initial value = Green).
You can also use the 'Init Value Configuration' editor for defining the preselected element of an enumeration.
A variable of the data type 'PossibleColors' would be initialized with the value Green (INT#1).
Further Info
See also the topic "User-defined data types". |