How Do I Specify a Conditional Instruction?
There are two different types of conditional evaluations in Statistica Visual Basic: the If...Then block and the Select Case block.
Utilizing an If...Then block.
The general syntax for an If...Then block is diagramed in the following structure:
If [Expression] Then
[Instruction]
ElseIf [Expression]Then
[Instruction]
Else
[Instruction]
End If
The components of the If...Then block are:
If...Then. This is the entry point for the conditional block. The corresponding expression will be evaluated, and if it is true then the respective instructions will be performed and the conditional block will be exited; however, if it is false then the conditional block will continue and the respective instructions will not be executed.
ElseIf...Then. This will be the next expression to be evaluated in the block. It will not be evaluated unless the first expression is found to be false. This is optional and is not necessary if only a single expression needs to be evaluated. Note, you must have a corresponding If to use ElseIf.
The If...Then block is the most common and flexible way to evaluate a condition and execute instructions if certain conditions are met. The following example demonstrates the syntax of an If block:
Sub main
'create a report and make it visible Dim r As New Report r.Visible = True 'evaluate whether or not it's visible 'if it is visible If r.Visible Then
MsgBox "The Report is visible"
'else if it is not visible ElseIf Not(r.Visible) Then
MsgBox "The Report is invisible"
'if nothing above was true then... Else
MsgBox _ "The Report is neither visible or invisible"
'indicate that the If block is done End If
End Sub
See Advanced conditional expressions for additional If...Then features.
Utilizing a Select Case block.
The general syntax for a Select Case block is diagramed in the following structure:
Select Case [Variable]
Case [FirstExpression]
[Instruction]
Case [SecondExpression]
[Instruction]
Case Else
[Instruction]
End Select
The components of the Select Case block are:
Case FirstExpression. This will be any data value that the evaluated variable may represent. If they are found to be the same then the corresponding instructions will be performed and the conditional block will be exited; otherwise, the next Case in the conditional block will be compared with the variable.
A Select Case is much easier to read and organize when compared to a rather lengthy If...Then block; however there are limitations: a Select Case can only evaluate a single variable and cannot evaluate expressions. A Select Case is designed to evaluate the value of a specific variable and act upon it, but it cannot evaluate true or false expressions (e.g., whether a spreadsheet is empty or if a variable is either greater or less than a certain value). For these types of situations an If...Then block is recommended. The following is an example of a Select Case which evaluates the value of a variable called Number and outputs its value if found:
Sub main
Dim Number As Integer Number = 3 'Evalute the value of the variable Number Select Case Number
'Is the value of Number 1? Case 1
MsgBox "Number is 1"
'Is the value of Number 2? Case 2
MsgBox "Number is 2"
'Otherwise... Case Else
MsgBox "Number isn't 1 or 2"
End Select
End Sub