How to Use Enums in Case Structures in LabVIEW

Updated Jan 10, 2018

Reported In

Software

  • LabVIEW Base
  • LabVIEW Full
  • LabVIEW Professional

Issue Details

  • How do enums work with Case Structures?
  • Also 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?
  • Why do I get numbers when I wire an enum to my Case Structure?

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 its values. Right click in the case structure frame and select Add case for every value to create the same amount of cases as the values the enum holds.

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 seasons in the same case by typing them in its label. The string values of the enum are written in double quotes, and these can be joined by commas between them.

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".

 

 

When using enumerated types (enums), always 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 Case Structure in the top of the image shows numbers instead of string labels, since the enum constant has been modified in one of its values. Therefore, LabVIEW coerces all of them to a numeric type and makes the Case Structure expect just numeric values. When developing state machines, or Case Structures that will input enums, consider making a Type Definition for your enum, so you don't have to modify all the constants manually when making a change.

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.

WAS THIS ARTICLE HELPFUL?

Not Helpful