Using Enums in Labview Case Structures

Updated Nov 7, 2023

Reported In

Software

  • LabVIEW

Issue Details

  • How do enums work with Case Structures?
  • How do I duplicate a case and get to define which of the enum values are assigned to that duplicate case?
  • Can I have more than one value for a case?

Solution

An enumerated type (enum) is a list of string labels with corresponding integer values. It is handled as an unsigned integer in LabVIEW (U8, U16 - default, and U32).
For example, we can have an enum to hold the four weather seasons, in which case we'll have a numeric value for each one: 0-"spring", 1-"summer", 2-"autumn", and 3-"winter".


 

When you wire an enum to the case selector of a Case Structure, you are able to make different cases for each of the enum's values. To create a case for each of the enum values, right-click on the case structure frame and select Add case for every value.
For example, if the enum holds the four weather seasons, you can have four different cases for each value "spring", "summer", "autumn", and "winter".
Furthermore, you can have multiple enum items in the same case. In the case Selector label, the string values of the enum are represented as strings in double-quotes. If you would like to add multiple items to one case, you can add their names in quotes separated by commas.
For example, if we want our Case Structure to handle Spring and Autumn together, for being equinoxes; and Summer and Winter together for being solstices, we can have: Case 1 - "Spring","Autumn". Case 2 - "Summer","Winter".

Additional Information

  • The Enumerated Data Type (Enum) is different from the Ring Data Type, which is not covered in this article. For more information about its differences and use, refer to the document Difference between a Text Ring Control and an Enum.
  • When using enumerated types (enums), it is best practice to make a type definition of the control. Creating type definitions prevents you from needing to rewrite the code each time you add or remove an item from an enum. This way, when you modify one of them, you don't get your string values replaced by numbers in the Case Structure labels.
    • Note: The top Case Structure in the image below shows numbers instead of string labels because one the enum constant has been modified in one of its values. Therefore, LabVIEW coerces all of the case labels to a numeric type and makes the Case Structure expect just numeric values.