Case
Description: | Selects one of a set of parameters for execution and returns its return value. |
Returns: | Numeric |
Usage: | Script or Steady State |
Function Groups: | Logic Control |
Related to: | ?: (If Else) | Cond | Execute | IfElse | IfThen |
Format: | Case(Index, P0, P1, P2) |
Parameters: |
Index |
Required. Any numerical expression giving the parameter number result to return. If this value is 0, the value of parameter P0 is returned. |
P0, P1, P2, … |
Required. The statements to be selected by the index. Only the selected statement value will be returned. |
Comments: |
The return value of this function is the return value of the parameter selected by Index, or invalid if index does not correspond to a parameter. |
Example:
If 1 Main; [ Case(pumpNum { Number varies from 0 to 3 }, { 0 }pumpDesc = "Pump 0", { 1 }Execute(pumpDesc = "Pump 1", Diesel = TRUE), { 2 }pumpDesc = "Pump 2", { 3 }pumpDesc = "Pump 3"); ]
This sets the pumpDesc variable according to pumpNum and in the second case, uses the Execute function to accomplish more than one task based on the value of pumpNum. If setting pumpDesc was all that was needed to be done, the statement could have been shortened to:
If 1 Main; [ pumpDesc = Case(pumpNum, { 0 } "Pump 0", { 1 } "Pump 1", { 2 } "Pump 2", { 3 } "Pump 3"); ]
Note that this example is written as English-only for clarity. For a multilingual application, each possible description should be phrase key.
Example: A variety of expressions
The following emphasizes that the Case function can take a variety of expressions.
Result = Case(Index, y = x + 1, "Two", StrCmp("a", "b"));
If run in steady state, all three expressions will evaluate, but only result from the one matching Index is passed to Result.
Example: Used in a steady-state expression within a text widget
Case([PumpSelection], { 0 } "Pump 0", { 1 } "Pump 1", { 2 } "Pump 2", { 3 } "Pump 3")
Example: Replacing a nested IfElse block
Instead of writing the following: (the various modes are enumerated constants)
Value = \DataMode == \#AVG_MODE ? AverageData[RowNumber] : \DataMode == \#MIN_MODE ? MinData[RowNumber] : \DataMode == \#MAX_MODE ? MaxData[RowNumber] : \DataMode == \#RAW_MODE ? RawValue : Invalid;
Use a Case statement:
Value = Case(\DataMode, AverageData[RowNumber], MinData[RowNumber], MaxData[RowNumber], RawValue);