Jump to content

Case structure: Difference between revisions

From LabVIEW Wiki
Add "See also" and "External links"
m Best practice: Link to actual page free label
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Underconstruction}}
{{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 22: 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 31: 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]]
Integer cases can have ranges. These ranges can be open ''(3..)'' or closed ''(3..10)''. Multiple ranges can be connected by a comma ''(3..10, 12..)''. Ranges are automatically sorted in ascending order by LabVIEW.
Integer cases can have ranges.
 
{| class="wikitable"
! Type
! Selector
! Description
|-
| Range (open)
| <tt>3..</tt>
| Executes if the value is greater than or equal to <tt>3</tt>
|-
| Range (closed)
| <tt>3..10</tt>
| Executes if the value is greater than or equal to <tt>3</tt> and lower than or equal to <tt>10</tt>
|-
| List
| <tt>3, 5, 7</tt>
| Executes if the value is either <tt>3</tt>, <tt>5</tt> or <tt>7</tt>
|}
 
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 ====
 
[[File:Case Structure - Enum.png|thumb|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 =====
 
[[File:Case Structure - Enum - Range Selector.png|thumb|Use range selectors with Enum Case Structures]]
Enum Case Structures support range selectors similar to the [[#Integer Case Structure|Integer Case Structure]]:


== Enum Case Structure ==
{| class="wikitable"
! Type
! Selector
! Description
|-
| Range (open)
| <tt>"First"..</tt>
| Executes if the value is greater than or equal to <tt>"First"</tt>.
|-
| Range (closed)
| <tt>"First".."Third"</tt>
| Executes if the value is greater than or equal to <tt>"First"</tt> and lower than or equal to <tt>"Third"</tt>.
|-
| List
| <tt>"First", "Third"</tt>
| Executes if the value is <tt>"First"</tt> or <tt>"Third"</tt>.
|}


[[File:Case Structure - Enum.png|thumb|Case Structure with Enum value]]
Values are automatically sorted by their Integer representation and not by name.


The [[Enum]] case structure uses the string interpretation of the value to label the cases.
===== Add Case for Every Value =====


=== Tricks ===
[[File:Case Structure - Enum - Add Case for Every Value.png|thumb|Add Case for Every Value]]


* To have a case for every Enum value right click on the selector label of the case structure and select ''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.
* Don't add a ''Default'' case, if the Enum datatype changes the code will break and you'll be triggered that a case should be added.
* A Tab-control's datatype is an Enum as well, so it's possible to connect a tab control directly to a case structure.


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


The string case structure uses the string value to select which case should execute.
The string Case Structure uses a case-sensitive string as selector value.
 
===== Case Insensitive Match =====
 
[[File:Case Structure - String - Case Insensitive Match.png|thumb|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 "<tt>A=a</tt>" at the bottom left corner if Case Insensitive Match is enabled.


=== Tricks ===
[[File:Case Structure - String - Case Insensitive Match - Indication.png|frameless|border]]


* Selection is by default done ''case sensitive'', to make the selection ''case insensitive'' right click on the selector label and select ''Case insensitive match''
===== Range selector =====
* 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 <tt>"foo".."foo~"</tt>. By adding the tilde on the end of the range you will get all strings starting with <tt>foo</tt> up to <tt>foo~</tt>. The <tt>tilde</tt> is the last visible ASCII character.
[[File:Case Structure - String - Range Selectors.png|thumb|Using range selectors on string type Case Structures]]
String Case Structures support ranges similar to [[#Integer Case Structure|Integer Case Structures]], but with added complexity due to the nature of strings. Note that ranges are matched using the [[Wikipedia:ASCII|ASCII]] character set.


==== Caveats for using ranges ====
{| class="wikitable"
! Type
! Selector
! Description (standard)
! Description (Case Insensitive Match)
|-
| Exact match
| <tt>"a"</tt>
| Executes if the value is exactly <tt>"a"</tt>
| Executes if the value is either <tt>"a"</tt> or <tt>"A"</tt>
|-
| Range (open)
| <tt>"a"..</tt>
| Executes if the value is greater than or equal to <tt>"a"</tt>
| Executes if the value is greater than or equal to <tt>"A"</tt> (lower on the ASCII table)
|-
| Range (closed)
| <tt>"a".."z"</tt>
| Executes if the value is greater than or equal to <tt>"a"</tt> and lower than <tt>"z"</tt> (not including <tt>"z"</tt>)
| Executes if the value is greater than or equal to <tt>"A"</tt> and lower than <tt>"z"</tt> (not including <tt>"z"</tt>)
|-
| List
| <tt>"a", "o", "z"</tt>
| Executes if the value is either <tt>"a"</tt>, <tt>"o"</tt> or <tt>"z"</tt>
| Executes if the value is either <tt>"A"</tt>, <tt>"a"</tt>, <tt>"O"</tt>, <tt>"o"</tt>, <tt>"Z"</tt> or <tt>"z"</tt>
|}


*The range of <tt>"UI:".."UI:Z"</tt> is not inclusive of <tt>"UI:Z"</tt>. To be inclusive you need to explicitly add it like this: <tt>"UI:".."UI:Z", "UI:Z"</tt> -- see [http://zone.ni.com/reference/en-XX/help/371361E-01/lvhowto/creating_case_structs/ docs] for details. Using <tt>tilde</tt> will not match the value with the <tt>tilde</tt>
Ranges are automatically sorted in ascending order by LabVIEW.
*If you wanted to include frames like <tt>"UI: Zoom Out"</tt> (that have more characters than just <tt>UI: Z</tt>), then you'd probably want to set your range to something like <tt>"UI:".."UI:zzzzzzzzzzzzz"</tt> (Note: I used lowercase "z" on purpose -- see next point) or the <tt>tilde</tt>.
*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 <tt>"UI:".."UI:zzzzzzzzzzzzzz"</tt>, since <tt>"z"</tt> (0x7A) &gt; <tt>"Z"</tt> (0x5A) -- see [http://zone.ni.com/reference/en-XX/help/371361E-01/lvhowto/creating_case_structs/ docs] for details.<br>


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


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.
 
===== Multiple error cases =====
 
[[File:Case Structure - Error Cluster - Multiple Error Cases.png|thumb|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 <tt>code</tt> 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:
 
{| class="wikitable"
! Type
! Selector
! Description
|-
| All Errors
| <tt>Error ..</tt>
| Executes for any error
|-
| Range (open)
| <tt>Error 3..</tt>
| Executes if the error code is greater than or equal to <tt>3</tt>
|-
| Range (closed)
| <tt>Error 3..10</tt>
| Executes if the error code is greater than or equal to <tt>3</tt> and lower than or equal to <tt>10</tt>
|-
| List
| <tt>Error 3, 5, 7</tt>
| Executes if the error code is either <tt>3</tt>, <tt>5</tt> or <tt>7</tt>
|}


Starting in [[LabVIEW 2019]] there can be more than one <tt>error case</tt> in an [[Error Case Structure|error case structure]]. In this case the selector uses the <tt>error code</tt> of an [[Error cluster]] to differentiate the cases. Each of the cases are still red.
A ''Default'' case is required when using error ranges.
<br clear="all">
 
=== Subdiagram Label ===
 
[[File:Case Structure - Show Subdiagram Label.png|thumb|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''.


== General Items  ==
[[File:Case Structure - Subdiagrams with Subdiagram Label.png|frameless|border]]


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 <tt>,</tt> (comma) between the values. A range of these values can be set by providing <tt>..</tt> (two dots) between the values.
While the position of the label is fixed, its height can be adjusted for each Subdiagram.


== Editing tips and tricks  ==
=== Keyboard shortcuts ===


*To create a new case after the current case use <tt>&lt;shift-enter&gt;</tt>, to duplicate the current case to a new case use <tt>&lt;ctrl-shift-enter&gt;</tt>
* <tt>&lt;Shift-Enter&gt;</tt> '''creates''' a new case '''after''' the current case.
*You can right click on the selecter and sort or switch cases
* <tt>&lt;Ctrl-Shift-Enter&gt;</tt> '''duplicates''' the current case.


== Best practices ==
== Best practice ==


* (only LabVIEW 2018 and lower) 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).
* (only LabVIEW 2019 or higher) Use [[Map|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|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.
* Set output terminals to [[Use Default Value|'''Use Default Value''']] if only some cases need to return values.
* Set output terminals to [[Use Default Value|'''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 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 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 [[Functions Palette/Programming/Structures/Diagram Disable Structure|Diagram Disable Structure]] instead.
* Do not use Case Structures to disable code (i.e. by wiring a False constant), use the [[Functions Palette/Programming/Structures/Diagram Disable Structure|Diagram Disable Structure]] instead.
== Tips and tricks ==
* Wire an error cluster to a Case Structure to turn it into an [[#Error Case Structure|Error Case Structure]].
== History ==
{| class="wikitable"
! Version
! Change(s)
! Comment
|-
| [[File:LV2019.png|frameless|border|64x64px|LabVIEW 2019|link=LabVIEW 2019]]
| Added support for multiple <tt>error cases</tt> in [[#Error Case Structure|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. [https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Allow-Error-Case-Structure-to-Handle-Specific-Error-Numbers/idi-p/1056452]
|-
| [[File:LV2015.png|frameless|border|64x64px|LabVIEW 2015|link=LabVIEW 2015]]
| Added an indicator for the Case Structure if [[#Case Insensitive Match|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. [https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Case-structure-with-case-sensitive-insensitive-indicator/idc-p/3165756]
|}


== See also ==
== See also ==


* [[CaseStructure class]]
* [[State Machine]]
* [[State Machine]]


Line 116: Line 240:


* [http://www.ni.com/getting-started/labview-basics/execution-structures#Case%20Structures National Instruments: Execution Structures in LabVIEW]
* [http://www.ni.com/getting-started/labview-basics/execution-structures#Case%20Structures National Instruments: Execution Structures in LabVIEW]
* [[Wikipedia: State machine (LabVIEW programming)]]


[[Category:Case Structure]]
[[Category:Case Structure]]
[[Category:Structures]]
[[Category:Structures Palette]]

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