PickValid

(Engine-Level Function)

Description: Attempts to return a valid value given a list of parameters.
Returns: Varies
Usage: Script or steady state.
Function Groups: Logic Control
Related to: Valid
Format: PickValid(Parm1, Parm2 [, Parm3, ...])
Parameters:  
Parm1, Parm2, Parm3...
Required. Any number of parameters giving any expressions, from which the first valid value will be selected.
Comments:

This function continues its search through its parameter list in order until the first valid value is found. If no valid parameters are supplied, INVALID is returned.

Use True(X) instead of PickValid(X, False). This is simpler and cleaner.

If executed in a script, after the first valid parameter is found, no further parameters are evaluated.

If executed in steady state, all parameters are always evaluated.

A relevant example might be an attempt to display one of two objects, depending on which is valid:


PickValid(Obj1.Widget(), Obj2.Widget());

In steady state, even if Obj1 is valid, both widgets will run. For the desired behavior, you should instead use code such as the following:

PickValid(Obj1.Widget, Obj2.Widget)();

 

Examples:

  A = Invalid;
  B = 83;
  C = Invalid;
  D = -1;
  ValidAns1 = PickValid(A, B, C, D);
  ValidAns2 = PickValid(A, C, D, B);

The value of ValidAns1 and ValidAns2 will be 83 and -1 respectively.

A typical use for this function is in setting default values as follows:

<
MyGraphic
(
  ParmLeft;
)
[
  Left       { local value for the left edge };
]
SetDefaults [ 
  If 1 Main;
  [
    Left = PickValid(ParmLeft, 100);
  ]
]
Main...
>

Given that module MyGraphic draws something in the window, adding the PickValid statement ensures that even if an invalid value is given as a parameter, the box will still be displayed, because its invalid parameter will have been replaced with the default value of 100.

 

Note that a local parameter is defined for Left, rather than using the parameter, parmLeft. This is necessary because, if the value passed to the module's parameter had been defined as a constant Invalid, it would not be possible to redefine it within MyGraphic using PickValid or any other statement.