Tuesday, March 4, 2008

Select Case

We will finish Chapter 5.3 on 3/6 which ends with Select Case. Remember that Select Case is just another structure that works like If statements. For example:

Dim num As Integer
num = CInt(txtIn.Text)
Select Case num
Case 1
MsgBox("You entered 1",,"")

Case 2 To 5
MsgBox("You entered a number from 2-5",,"")

Case 7,9,11
MsgBox("You entered an odd number from 7-11",,"")

Case Is > 12
MsgBox("You entered a number greater than 12",,"")

Case Else
MsgBox("You entered some other number",,"")

End Select

All of this works like the classic If/ElseIf/Else structure. so we could rewrite the above using If statements:

Dim num As Integer
num = CInt(txtIn.Text)
If num = 1 Then
MsgBox("You entered 1",,"")
ElseIf 2 <= num And num <= 5 Then
MsgBox("You entered a number from 2-5",,"")
ElseIf num = 7 Or num = 9 Or num = 11 Then
MsgBox("You entered an odd number from 7-11",,"")
ElseIf num > 12 Then
MsgBox("You entered a number greater than 12",,"")
Else
MsgBox("You entered some other number",,"")
End If


You can also use Select Case to compare Strings just like you can with If statements.

No comments: