This blog post describes the creation of enumeration types, the usage of enumeration and interfacing enumeration with integers. Using enumeration
Definition of an enumeration type
An enumeration type is a data type that consists of a number of elements (i.e. a list). A variable of this enumeration type can be set to any of the elements of that enumeration type (i.e. to any of the items on the list).
This allows the user to create a variable whose value can only be set to an item from a list. This restricts the user inputs to a number of valid settings (hence reducing errors) and makes the code easier to read.
Creating an enumeration type
Enumeration types can be created by using File->New-> Type (enumeration).
Figure 1. Creating TrafficLightSignals enumeration type with File->New->Type
The enumeration type created in Figure 1 does not contain any elements (i.e. it is an empty list). To add elements select the enumeration class and press Ctrl+N or Edit->Enumeration->New Element.
Figure 2. Adding elements to the TrafficLightSignals enumeration type using Edit->Enumeration->New Element. Elements can also be added by selecting the TrafficLightSignals enumeration type and pressing Ctrl+N
After the Green, Orange the and Red elements are added to the TrafficLightSignals enumeration type, the code generated is :
Figure 3. Enumeration type code that was generated using the GUI interface as shown in Figure 1 and Figure 2.
If an error is made while adding elements to the enumeration list, you can edit the list by going to Edit->Enumeration->Edit Enumeration or by going to the text layer and manually editing the code in the text layer (as shown in Figure 3).
Making use of an enumeration type in an example
The example in Figure 4 makes use of a variable called trafficLightSignal of enumeration type EnumerationExample.TrafficLightSignals.
Figure 4. Model that makes use of an enumeration type variable
A variable that is an enumeration type can be set to elements of the enumeration list as in the TrafficLights model as in Figure 4. The result of the enumeration variable in Figure 4 is plotted in Figure 5.
Figure 5. Plot of trafficLightSignal enumeration type variable. Vertical axis contains element’s descriptions.
Associated integer value
An enumeration signal (for example trafficLightSignal in the TrafficLights model) stores the information as an Integer with 1 being the first element in the list (i.e. Green) 2 being the second and etc. So the plot in figure 5 could be seen as Green being 1, Orange being 2 and Red being 3. To get which integer value is associated with which enumeration element use Integer() for example: Integer(EnumerationEample.TrafficLightSignals.Orange) returns 2.
The enumeration parameter scroll down list
When a parameter of an enumeration type is modified from a parameter dialog box, a pull down menu is present as in the example in Figure 6. Using enumeration Using enumeration
Figure 6. Drop down menu for parameter EnumerationExample.TrafficLightSignals startSignal;
Written by: Garron Fish – Chief Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion