Case structure

From LabVIEW Wiki
(Redirected from Case Structure)
Jump to: navigation, search
Object information
Owning palette(s) Structures palette
Type Structure
Requires Basic Development Environment
Icon Functions Palette - Programming - Structures - Case Structure.png
Components of a Case Structure

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.

Usage

Supported data types

The Case Structure automatically adapts to following data types:

Some features are only available for specific data types.

Boolean Case Structure

Case Structure with Boolean value

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

Case Structure with Integer value

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
Using range selectors on an Integer Case Structure

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
Change Radix on an Integer Case Structure

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

Case Structure with Enum value

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
Use range selectors with Enum Case Structures

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

Case Structure with String value

The string Case Structure uses a case-sensitive string as selector value.

Case Insensitive Match
Enable Case Insensitive Match

By default the Case Structure does a case sensitive match. This behavior can be changed by using the Case Insensitive Match option from the right-click menu.

From LabVIEW 2015 onward, the Case Structure shows "A=a" at the bottom left corner if Case Insensitive Match is enabled.

Case Structure - String - Case Insensitive Match - Indication.png

Range selector
Using range selectors on string type Case Structures

String Case Structures support ranges similar to Integer Case Structures, but with added complexity due to the nature of strings. Note that ranges are matched using the ASCII character set.

Type Selector Description (standard) Description (Case Insensitive Match)
Exact match "a" Executes if the value is exactly "a" Executes if the value is either "a" or "A"
Range (open) "a".. Executes if the value is greater than or equal to "a" Executes if the value is greater than or equal to "A" (lower on the ASCII table)
Range (closed) "a".."z" Executes if the value is greater than or equal to "a" and lower than "z" (not including "z") Executes if the value is greater than or equal to "A" and lower than "z" (not including "z")
List "a", "o", "z" Executes if the value is either "a", "o" or "z" Executes if the value is either "A", "a", "O", "o", "Z" or "z"

Ranges are automatically sorted in ascending order by LabVIEW.

Error Case Structure

Case Structure with Error Cluster

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
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. Error codes are handled like Integer values and therefore can specify range selectors:

Type Selector Description
All Errors Error .. Executes for any error
Range (open) Error 3.. Executes if the error code is greater than or equal to 3
Range (closed) Error 3..10 Executes if the error code is greater than or equal to 3 and lower than or equal to 10
List Error 3, 5, 7 Executes if the error code is either 3, 5 or 7

A Default case is required when using error ranges.

Subdiagram Label

Show Subdiagram Label

The Subdiagram Label adds a label at top of each Subdiagram. It is hidden by default and can be shown via the right-click menu option Visible Items >> Subdiagram Label.

Case Structure - Subdiagrams with Subdiagram Label.png

While the position of the label is fixed, its height can be adjusted for each Subdiagram.

Keyboard shortcuts

  • <Shift-Enter> creates a new case after the current case.
  • <Ctrl-Shift-Enter> duplicates the current case.

Best practice

  • Use Lookup Tables 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).
  • Use the Match Pattern function or Match Regular Expression function instead of range selectors for string Case Structures.
  • 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.

Tips and tricks

History

Version Change(s) Comment
LabVIEW 2019 Added support for multiple error cases in Error Case Structures. This feature was requested on the LabVIEW Idea Exchange by Hueter on 19.01.2010 and later confirmed by Darren to be included in LabVIEW 2019 on 30.05.2019. [1]
LabVIEW 2015 Added an indicator for the Case Structure if Case Insensitive Match is enabled. This feature was requested on the LabVIEW Idea Exchange by Matthias_H on ‎08.08.2014 and later confirmed by Darren to be included in LabVIEW 2015 on 17.08.2015. [2]

See also

External links