Jump to content

Case structure: Difference between revisions

From LabVIEW Wiki
m Add to Structures Palette
m Best practice: Link to actual page free label
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{TOCright}}
{{LabVIEW Palette Object Information|palette=Functions Palette/Programming/Structures{{!}}Structures palette|type=structure|icon=Functions Palette - Programming - Structures - Case Structure.png}}
[[File:Case Structure - Components.png|thumb|Components of a Case Structure]]
[[File:Case Structure - Components.png|thumb|Components of a Case Structure]]


A Case Structure is a primitive [[Functions Palette/Programming/Structures|Structure]] that can have multiple [[Subdiagram|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 [[:Category:Data types|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.
A '''Case Structure''' is a primitive [[Functions Palette/Programming/Structures|Structure]] that can have multiple [[Subdiagram|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 [[:Category:Data types|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 ==
== Usage ==
=== Supported data types ===


The Case Structure automatically adapts to following data types:
The Case Structure automatically adapts to following data types:


* [[Boolean|Boolean]]
* [[Boolean data type|Boolean]]
* [[Integer|Integer]]
* [[Integer data type|Integer]]
* [[Enum|Enum]]
* [[Enumerated type|Enum]]
* [[String|String]]
* [[String data type|String]]
* [[Error Cluster|Error Cluster]]
* [[Error Cluster data type|Error Cluster]]


Some features are only available for specific data types.
Some features are only available for specific data types.
<br clear="all">
<br clear="all">
== Boolean Case Structure ==
==== Boolean Case Structure ====


[[File:Case Structure - Boolean.png|thumb|Case Structure with Boolean value]]
[[File:Case Structure - Boolean.png|thumb|Case Structure with Boolean value]]
Line 21: Line 24:
The Boolean Case Structure is the default Structure when placed from the [[Functions Palette]]. It has exactly two cases: ''True'' and ''False''.
The Boolean Case Structure is the default Structure when placed from the [[Functions Palette]]. It has exactly two cases: ''True'' and ''False''.


<br clear="all">
==== Integer Case Structure ====
== Integer Case Structure ==


[[File:Case Structure - Integer.png|thumb|Case Structure with Integer value]]
[[File:Case Structure - Integer.png|thumb|Case Structure with Integer value]]
Line 30: Line 32:
Complex numbers ([[Complex Single]], [[Complex Double]], [[Complex Extended]]) are not supported and will break the VI.
Complex numbers ([[Complex Single]], [[Complex Double]], [[Complex Extended]]) are not supported and will break the VI.


<br clear="all">
===== Range selector =====
=== Range selector ===


[[File:Case Structure - Integer - Range Selectors.png|thumb|Using range selectors on an Integer Case Structure]]
[[File:Case Structure - Integer - Range Selectors.png|thumb|Using range selectors on an Integer Case Structure]]
Line 56: Line 57:
Ranges are automatically sorted in ascending order by LabVIEW.
Ranges are automatically sorted in ascending order by LabVIEW.


<br clear="all">
===== Radix =====
 
=== Radix ===


[[File:Case Structure - Integer - Change Radix.png|thumb|Change Radix on an Integer Case Structure]]
[[File:Case Structure - Integer - Change Radix.png|thumb|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''.
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''.


<br clear="all">
==== Enum Case Structure ====
 
== Enum Case Structure ==


[[File:Case Structure - Enum.png|thumb|Case Structure with Enum value]]
[[File:Case Structure - Enum.png|thumb|Case Structure with Enum value]]
Line 71: Line 68:
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.
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.


<br clear="all">
===== Range selector =====
=== Range selector ===


[[File:Case Structure - Enum - Range Selector.png|thumb|Use range selectors with Enum Case Structures]]
[[File:Case Structure - Enum - Range Selector.png|thumb|Use range selectors with Enum Case Structures]]
Line 97: Line 93:
Values are automatically sorted by their Integer representation and not by name.
Values are automatically sorted by their Integer representation and not by name.


<br clear="all">
===== Add Case for Every Value =====
 
=== Add Case for Every Value ===


[[File:Case Structure - Enum - Add Case for Every Value.png|thumb|Add Case for Every Value]]
[[File:Case Structure - Enum - Add Case for Every Value.png|thumb|Add Case for Every Value]]
Line 105: Line 99:
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.
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.


<br clear="all">
==== String Case Structure ====
 
== String Case Structure ==


[[File:Case Structure - String.png|thumb|Case Structure with String value]]
[[File:Case Structure - String.png|thumb|Case Structure with String value]]
Line 113: Line 105:
The string Case Structure uses a case-sensitive string as selector value.
The string Case Structure uses a case-sensitive string as selector value.


<br clear="all">
===== Case Insensitive Match =====
=== Case Insensitive Match ===


[[File:Case Structure - String - Case Insensitive Match.png|thumb|Enable Case Insensitive Match]]
[[File:Case Structure - String - Case Insensitive Match.png|thumb|Enable Case Insensitive Match]]
Line 124: Line 115:
[[File:Case Structure - String - Case Insensitive Match - Indication.png|frameless|border]]
[[File:Case Structure - String - Case Insensitive Match - Indication.png|frameless|border]]


<br clear="all">
===== Range selector =====
 
=== Range selector ===


[[File:Case Structure - String - Range Selectors.png|thumb|Using range selectors on string type Case Structures]]
[[File:Case Structure - String - Range Selectors.png|thumb|Using range selectors on string type Case Structures]]
Line 160: Line 149:
Ranges are automatically sorted in ascending order by LabVIEW.
Ranges are automatically sorted in ascending order by LabVIEW.


<br clear="all">
==== Error Case Structure ====
 
== Error Case Structure ==


[[File:Case Structure - Error Cluster.png|thumb|Case Structure with Error Cluster]]
[[File:Case Structure - Error Cluster.png|thumb|Case Structure with Error Cluster]]
Line 168: Line 155:
The [[Error Case Structure|error case structure]] is like the Boolean case structure but can be controlled by the <tt>error state</tt> of an [[Error cluster]]. The border of the <tt>error case</tt> is red and the <tt>non-error case</tt> is green.
The [[Error Case Structure|error case structure]] is like the Boolean case structure but can be controlled by the <tt>error state</tt> of an [[Error cluster]]. The border of the <tt>error case</tt> is red and the <tt>non-error case</tt> is green.


<br clear="all">
===== Multiple error cases =====
=== Multiple error cases ===


[[File:Case Structure - Error Cluster - Multiple Error Cases.png|thumb|Multiple error cases]]
[[File:Case Structure - Error Cluster - Multiple Error Cases.png|thumb|Multiple error cases]]
Line 199: Line 185:
A ''Default'' case is required when using error ranges.
A ''Default'' case is required when using error ranges.


<br clear="all">
=== Subdiagram Label ===
== Subdiagram Label ==


[[File:Case Structure - Show Subdiagram Label.png|thumb|Show Subdiagram Label]]
[[File:Case Structure - Show Subdiagram Label.png|thumb|Show Subdiagram Label]]
Line 210: Line 195:
While the position of the label is fixed, its height can be adjusted for each Subdiagram.
While the position of the label is fixed, its height can be adjusted for each Subdiagram.


<br clear="all">
=== Keyboard shortcuts ===
== Keyboard shortcuts ==


* <tt>&lt;Shift-Enter&gt;</tt> '''creates''' a new case '''after''' the current case.
* <tt>&lt;Shift-Enter&gt;</tt> '''creates''' a new case '''after''' the current case.
Line 220: Line 204:
* Use [[Lookup Table|Lookup Tables]] instead of Case Structures to return values from a constant non-consecutive list of items (i.e. error code => error message).
* Use [[Lookup Table|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|range selector]] instead.
* If two cases perform the same operation, remove one case and use the [[#Range_selector|range selector]] instead.
* Use [[#Subdiagram_Label|Subdiagram labels]] instead of [[Free Label|free labels]] to document Subdiagrams.
* Use [[#Subdiagram_Label|Subdiagram labels]] instead of [[free label]]s to document Subdiagrams.
* Use [[Linked Input Tunnel|Linked Input Tunnels]] for data wires that '''must''' be wired through all cases (changed or unchanged).
* Use [[Linked Input Tunnel|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.
* Use the [[Match Pattern function]] or [[Match Regular Expression function]] instead of range selectors for string Case Structures.

Latest revision as of 17:03, 6 May 2023

Object information
Owning palette(s) Structures palette
Type Structure
Requires Basic Development Environment
Icon
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.

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.

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