Conditional Statements (If, Select/Case) |
Top Previous Next |
If statement
If/Then/Else statement is used to execute code conditionally. The Else part can be omit. Here some examples:
If X > 0 Then MsgBox("X is positive") End If
If S <> "" Then MsgBox("S is not empty") Else MsgBox("S is empty") End If
If S <> "" Then MsgBox("S is not empty") Return True Else MsgBox("S is empty") End If
If statement has also a shorter single-line version, that should be written without End If:
If X > 0 Then MsgBox("X is positive") If X > 0 Then MsgBox("X is positive") Else MsgBox("X is negative or zero")
Select/Case statement
The Select statement is another statement for conditional code execution. Unlike MS VbScript, in NativeVB you can specify any expressions in case labels, not only constant literals. Also, NativeVB does not check values uniqueness. Just as in If statement, Else part can be omit. Examples:
Select Case x Case 0: MsgBox("Zero") Case 1, 3, 5, 7, 9: MsgBox("Odd") Case 2, 4, 6, 8, 10: MsgBox("Even") Case Else MsgBox("I'm too young to know numbers greater than 10") End Select
The Case Else label, if present, should be the last label in the Select statement. Since any expressions in case labels are allowed, you can also specify, for example, string expressions:
Select Case s Case "True": b := True; Case "False": b := False; Case Else MgsBox("Invalid value"); End Select |