Switch

Navigation:  Widget Designer > Script Language > Programming Statements >

Switch

prev main next

Navigation:  Widget Designer > Script Language > Programming Statements >

Switch

prev main next

Loading...

The switch statement (also known as multi way branches) can be used to program nested if statements more easily and with a better overview. Please refer to the previous chapter if-then-else statements.

Basically, the statement starts with looking at a specific variable, the so-called "Input Value". It needs to be declared beforehand. The following Cases (i.e command blocks) contain an individual value, the "Case Value". Each Case is evaluated, that is, it is compared whether the Input Value equals Case Value. If this condition is "true", the Case Script is executed.

The first Case could be "If the above variable equals value A then execute script A". All following Cases would be "If it equals my value then execute my script". The entire select-case statement searches for the Cases whose value equals the Input Value (the above variable) and executes the according scripts.

- A Case Value can be a variable itself.

- A Case Script starts and ends with curly brackets {} and contains an unlimited number of commands including functions and macros.

- The number of Cases is unlimited.

- If there is no True Case (command block containing an equal value), an optional default script can be defined. This block starts with "CaseElse".

Even encapsulated structures can be used, containing further switch cases, for-loops or if-else-statements.

Please note that you can also use mathematical expressions as Input or Case Values.

Basic syntax

Switch YourVariable {

 

Case VariableValue1

 Value1 Script

 

Case VariableValue2

 Value2 Script

 

Case VariableValueN

 ValueN Script

 

Case Else

 Default Script

}

 

It does not matter whether the curly brackets are in the same or next line, but it is strongly recommended to add new lines and also tabs for a clear and easy to read arrangement.
Code blocks can be in/out-dented using [Tab] or [Shift + Tab].

If you would like to comment a line out without getting errors in the Debug Logger, use this syntax:
// one line that should not be processed
/* several lines

that should not

be processed */

 

Script Example

This example is based on the string variable "VarName". It holds the forename of a customer who has logged in before. The idea is, that a Label displays a text with the country, the customer comes from and in addition the Widget Designer calls a page containing regional information and control elements.
Three Cases are programmed, one for "Daniel", one for "Rene" and one for "Rajesh".
Afterwards, a "Case Else"  is defined as it is possible that unknown persons log in. In that case, he is routed to a Home Screen and the Label displays "Please register".

Switch VarName {

 

Case "Daniel"

 Label1.Text = "from Finland"

 WDPageGoto("Europe")

 

Case "Rene"

 Label1.Text = "from France"

 WDPageGoto("Europe")

 

Case "Rajesh"

 Label1.Text = "from India"

 WDPageGoto("Asia")

 

Case Else

 Label1.Text = "Please register..."

 WDPageGoto("Home")

}

 

Alternatively, the "Case Else" block can be eliminated from the statement if it is not needed. Or, the Page "Home" is called at the very beginning of the entire script, even before the main variable is selected. Different solutions are possible and depend on your project.