Case structure
| |
This page is under construction. This page or section is currently in the middle of an expansion or major revamping. However, you are welcome to assist in its construction by editing it as well. Please view the edit history should you wish to contact the person who placed this template. If this article has not been edited in several days please remove this template. Please don't delete this page unless the page hasn't been edited in several days. While actively editing, consider adding {{inuse}} to reduce edit conflicts. |

A Case Structure is a primitive Structure that can have multiple Subdiagrams (also known as "Cases"), one of which is selectively executed at runtime. A selector value determines which case is executed at runtime. The Structure autmatically adapts to the data type connected to the Case selector and must have exactly one Subdiagram for each possible value of the connected data type, or a Default case to handle undefined values.
Supported data types
The Case Structure automatically adapts to following data types:
Some features are only available for specific data types.
Boolean Case Structure

The Boolean Case Structure is the default Structure when placed from the Functions Palette. It has exactly two cases: True and False.
Integer Case Structure

The Integer Case Structure uses an Integer as selector value and can have as many cases as the specific Integer type supports. Non-integer values (Fixed-point, Single Precision, Double Precision, Extended Precision) are rounded according to IEEE 754-1985. Complex numbers (Complex Single, Complex Double, Complex Extended) are not supported and will break the VI.
Range selector

Integer cases can have ranges.
| Type | Selector | Description |
|---|---|---|
| Range (open) | 3.. | Executes if the value is greater than or equal to 3 |
| Range (closed) | 3..10 | Executes if the value is greater than or equal to 3 and lower than or equal to 10 |
| List | 3, 5, 7 | Executes if the value is either 3, 5 or 7 |
Ranges are automatically sorted in ascending order by LabVIEW.
Radix

The default representation for Integer values is Decimal. This representation can be changed to Decimal, Hex, Octal or Binary from the right-click menu option Radix.
Enum Case Structure

The Enum Case Structure uses the string interpretation of the value to label cases. Internally, however, LabVIEW uses the Integer representation to select cases. Renaming Enum values has no effect on the Case Structure.
Range selector

Enum Case Structures support range selectors similar to the Integer Case Structure:
| Type | Selector | Description |
|---|---|---|
| Range (open) | "First".. | Executes if the value is greater than or equal to "First". |
| Range (closed) | "First".."Third" | Executes if the value is greater than or equal to "First" and lower than or equal to "Third". |
| List | "First", "Third" | Executes if the value is "First" or "Third". |
Values are automatically sorted by their Integer representation and not by name.
Add Case for Every Value

Because Enums have a finite amount of values, LabVIEW provides a simple way to add cases for every value via the right-click menu option Add Case for Every Value. This automatically adds cases for all missing values.
String Case Structure

The string case structure uses the string value to select which case should execute.
Tricks
- Selection is by default done case sensitive, to make the selection case insensitive right click on the selector label and select Case insensitive match
- A default case is obliged
- Ranges
Just like integer and Enum case structures a string case structure accepts ranges. To detect all string starting with foo the following range would apply "foo".."foo~". By adding the tilde on the end of the range you will get all strings starting with foo up to foo~. The tilde is the last visible ASCII character.
Caveats for using ranges
- The range of "UI:".."UI:Z" is not inclusive of "UI:Z". To be inclusive you need to explicitly add it like this: "UI:".."UI:Z", "UI:Z" -- see docs for details. Using tilde will not match the value with the tilde
- If you wanted to include frames like "UI: Zoom Out" (that have more characters than just UI: Z), then you'd probably want to set your range to something like "UI:".."UI:zzzzzzzzzzzzz" (Note: I used lowercase "z" on purpose -- see next point) or the tilde.
- String range matches are case sensitive (even if your case structure is configured for Case Insensitive Match, I believe), so you'd want to use a range like "UI:".."UI:zzzzzzzzzzzzzz", since "z" (0x7A) > "Z" (0x5A) -- see docs for details.
Error Case Structure

The error case structure is like the Boolean case structure but can be controlled by the error state of an Error cluster. The border of the error case is red and the non-error case is green.
Multiple error cases

Since LabVIEW 2019 it is possible to have multiple error cases for specific error codes. Previously this was only possible by unbundling the code element from the Error Cluster and connect it to an Integer Case Structure inside the error case. Now this can be done without nested Case Structures. Error codes are handled like Integer values and therefore support the same range selectors as Integer Case Structures.
General Items
On compile time the compiler assures that at least one case will run when the code executes, if this cannot be determined the containing VI will be Broken. To provide a case for values of the selector that is general a case can be labeled Default. One case can be used to handle several selector values. To enter these provide a , (comma) between the values. A range of these values can be set by providing .. (two dots) between the values.
Keyboard shortcuts
- <Shift-Enter> creates a new case after the current case.
- <Ctrl-Shift-Enter> duplicates the current case.
Best practices
- (only LabVIEW 2018 and lower) Use Lookup Tables instead of Case Structures to return values from a constant non-consecutive list of items (i.e. error code => error message).
- (only LabVIEW 2019 or higher) Use Maps instead of Case Structures to return values from a constant non-consecutive list of items (i.e. error code => error message).
- If two cases perform the same operation, remove one case and use the range selector instead.
- Use Subdiagram labels instead of free labels to document Subdiagrams.
- Use Linked Input Tunnels for data wires that must be wired through all cases (changed or unchanged).
- Set output terminals to Use Default Value if only some cases need to return values.
- Do not connect data wires if they are not used inside the Case Structure. Those wires should be wired around the Structure.
- Do not add a Default case for Enum values if the Structure is supposed to handle all values at all times.
- Do not use Case Structures to disable code (i.e. by wiring a False constant), use the Diagram Disable Structure instead.
History
| Version | Change(s) |
|---|---|
| Added support for multiple error cases in Error Case Structures. |
