Switch

Command group Flag affected Reversible Execute on client Platform(s)
Constructs NO NO YES All

Syntax

Switch expression

Description

This command initiates a Switch method construct. You use a Switch statement to select a course of action from a set of options based on the value of a variable, expression or calculation. It is similar to an IfElse If construct although the performance of a Switch construct tends to be faster.

The first line of the construction contains the Switch command. This defines the variable, expression or calculation on which the choice of action will depend. Following the Switch command, the Case commands provide values which, if matched with the expression supplied in the Switch line, cause the methods between case lines to be executed.

You can use the Break to end of switch command to jump out of the current Case statement and resume method execution after the End Switch command. Note you cannot use the Break to end of loop command to break out of a Switch construct.

You can nest multiple Switch statements, and embed other conditional statements such as IfElse constructs.

Example

# next button - only allow user to proceed to next page of a paged pane if the required information has been entered
Calculate lPage as $cwind.$objs.PagedPane.$currentpage
Switch lPage
  Case 1
    If len(iSerialNumber)=0
      Calculate lErrorMsg as 'Please enter a serial number'
    End If
  Case 2
    If len(iUserName)=0
      Calculate lErrorMsg as 'Please enter your username'
    End If
  Case 3
    If iAgreeFlag=kFalse
      Calculate lErrorMsg as 'You may not proceed until you agree to the license agreement'
    End If
End Switch

If len(lErrorMsg)
  OK message {[lErrorMsg]}
Else
  Do $cwind.$objs.PagedPane.$currentpage.$assign(lPage+1) ## go to next page
End If