Case

(Engine-Level Function)

Description:

In script, this selects one of a set of parameters for execution and returns its return value.

In steady state all parameters are executed, but only the value of the one matching the case condition is returned.

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 Parm 0 is returned.
Parm 0, Parm 1, Parm 2, …
Required. The statements to be selected by the index. Only the selected statement value will be returned. The statement in each parameter could be an expression or a value.
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.

When used in steady state, all cases will execute on parameter change even if they are not the selected parameter statement. When used in script, only the selected parameter statement will execute.

Example:

Note that the format varies depending on whether you are writing script code or an expression. A scripting version comes first, with an expression version later.

  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 use simple values rather than expressions.

  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: (Used in a steady-state expression within a text widget - this example will not compile in a .SRC module.)

Used in a steady-state expression, the IF statement is removed, commas are used between each case, and there is no ending semicolon.

  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,
            { 0 } AverageData[RowNumber];
            { 1 } MinData[RowNumber];
            { 2 } MaxData[RowNumber];
            { 3 } RawValue;
          );