Jump to content

Case structure: Difference between revisions

From LabVIEW Wiki
m Added second case structure image
No edit summary
Line 1: Line 1:
{{TOCnestright}}  
{{TOCnestright}}


== Introduction  ==
== Introduction  ==


A case structure is a LabVIEW primitive that dynamically selects which parts of code should execute. [[Image:CaseStructure.PNG]]  
A case structure is a LabVIEW primitive that dynamically selects which parts of code should execute. [[Image:CaseStructure.PNG]]


== General Items  ==
== 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 <tt>default</tt>. 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.  
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 <tt>default</tt>. 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.


= Editing tips and tricks<br>  =
= Editing tips and tricks<br>  =


*To create a new case after the current case use <tt>&lt;ctrl-enter&gt;</tt>, to duplicate the current case to a new case use <tt>&lt;ctrl-shift-enter&gt;</tt>  
*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>
*You can right click on the selecter and sort or switch cases
*You can right click on the selecter and sort or switch cases


== Selection mechanism  ==
== Selection mechanism  ==


The selection decision of the case structure can be done by several datatypes:  
The selection decision of the case structure can be done by several datatypes:


*Boolean  
*Boolean
*Integer  
*Integer
*Enum  
*Enum
*String  
*String
*Error Cluster
*Error Cluster


=== Boolean  ===
=== Boolean  ===


The boolean case structure is the default state of the case structure. It has a case for the <tt>True</tt> and the <tt>False</tt> state.  
The boolean case structure is the default state of the case structure. It has a case for the <tt>True</tt> and the <tt>False</tt> state.


[[Image:BooleanCase.PNG]]  
[[Image:BooleanCase.PNG]]


=== Integer  ===
=== Integer  ===


The integer case structure uses the integer value of the selector data type to determine which case is executed  
The integer case structure uses the integer value of the selector data type to determine which case is executed


[[Image:IntegerCase.PNG]]  
[[Image:IntegerCase.PNG]]


==== Floats  ====
==== Floats  ====


Non-integer numerics (floats) are [[Rounding|rounded]] according to IEEE 754-1985, where a non-integer is rounded to the nearest even integer if it's rest is <tt>.5</tt> .  
Non-integer numerics (floats) are [[Rounding|rounded]] according to IEEE 754-1985, where a non-integer is rounded to the nearest even integer if it's rest is <tt>.5</tt> .


==== Tricks  ====
==== Tricks  ====


*Ranges<br>An integer case structure accepts ranges to limit the number of cases. These ranges can be closed <tt>(3..10)</tt> or open <tt>(3..)</tt> where every value above 2 is executed.  
*Ranges<br>An integer case structure accepts ranges to limit the number of cases. These ranges can be closed <tt>(3..10)</tt> or open <tt>(3..)</tt> where every value above 2 is executed.
*It's possible to show Hex, octal or binary values using Radix. Right-click on the number and select <tt>Radix</tt>.<br>
*It's possible to show Hex, octal or binary values using Radix. Right-click on the number and select <tt>Radix</tt>.<br>


[[Image:Example of radix in case structure.png]]  
[[Image:Example of radix in case structure.png]]


[[Image:BinaryCaseSelect.png]]  
[[Image:BinaryCaseSelect.png]]


=== Enum or Tab control<br> ===
=== Enum or Tab control<br> ===


The [[Enum]] case structure uses the string interpretation of the value to label the cases.  
The [[Enum]] case structure uses the string interpretation of the value to label the cases.


[[Image:EnumCase.PNG]]  
[[Image:EnumCase.PNG]]


==== Tricks  ====
==== Tricks  ====


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


=== String  ===
=== String  ===


The string case structure uses the string value to select which case should execute.  
The string case structure uses the string value to select which case should execute.


[[Image:StringCase.PNG]]  
[[Image:StringCase.PNG]]


==== Tricks  ====
==== Tricks  ====


*Selection is by default done <tt>case sensitive</tt>, to make the selection <tt>case insensitive</tt> right click on the selector label and select <tt>Case insensitive match</tt>  
*Selection is by default done <tt>case sensitive</tt>, to make the selection <tt>case insensitive</tt> right click on the selector label and select <tt>Case insensitive match</tt>
*A default case is obliged  
*A default case is obliged
*Ranges<br>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.
*Ranges<br>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.


===== Caveats for using ranges  =====
===== Caveats for using ranges  =====


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


=== Error  ===
=== Error  ===


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.


[[Image:ErrorCase.PNG]]  
[[Image:ErrorCase.PNG]]


[[Image:Error_Case_Structure.png]]  
[[Image:Error_Case_Structure.png]]


[[Category:Structures]]
[[Category:Structures]]

Revision as of 18:59, 2 November 2012

Introduction

A case structure is a LabVIEW primitive that dynamically selects which parts of code should execute.

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.

Editing tips and tricks

  • To create a new case after the current case use <shift-enter>, to duplicate the current case to a new case use <ctrl-shift-enter>
  • You can right click on the selecter and sort or switch cases

Selection mechanism

The selection decision of the case structure can be done by several datatypes:

  • Boolean
  • Integer
  • Enum
  • String
  • Error Cluster

Boolean

The boolean case structure is the default state of the case structure. It has a case for the True and the False state.

Integer

The integer case structure uses the integer value of the selector data type to determine which case is executed

Floats

Non-integer numerics (floats) are rounded according to IEEE 754-1985, where a non-integer is rounded to the nearest even integer if it's rest is .5 .

Tricks

  • Ranges
    An integer case structure accepts ranges to limit the number of cases. These ranges can be closed (3..10) or open (3..) where every value above 2 is executed.
  • It's possible to show Hex, octal or binary values using Radix. Right-click on the number and select Radix.

Enum or Tab control

The Enum case structure uses the string interpretation of the value to label the cases.

Tricks

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

String

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

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.