• No results found

The Select Case Statement Introduction

In document Microsoft Visual Basic 2010 Tutorial (Page 198-200)

If you have a large number of conditions to examine, the If...Then...Else statement will go through each one of them. The Visual Basic language offers the alternative of jumping to the statement that applies to the state of the condition. This is done with the Select and Case keywords.

The formula of the Select Case statement is:

Select Case Expression Case Expression1 Statement1 Case Expression2 Statement2 Case ExpressionX StatementX End Select

The statement starts with Select Case and ends with End Select. On the right side of Select Case, enter a value, the Expression factor, that will be used as a tag. The value of Expression can be Boolean value (a Boolean type), a character (Char type), a string (a String type), a natural number (a Byte, an SByte, a Short, a UShort, an Integer, a UInteger, a Long, or a ULong type), a decimal number (a Single, a Double, or a Decimal type), a date or time value (a Date type), an enumeration (an Enum type), or else (an Object type). Inside the Select Case and the End Select lines, you provide one or more sections that each contains a Case keyword followed by a value. The value on the right side of Case, ExpresionX, must be the same type as the value of Expression or it can be implied from it. After the case and its expression, you can write a statement.

When this section of code is accessed, the value of Expression is considered. Then the value of Expression is compared to each ExpressionX of each case:

a. If the value of Expression1 is equal to that of Expression, then Statement1 is executed. If the value of Expression1 is not equal to that of Expression, then the compiler moves to Expression2 b. If the value of Expression2 is equal to that of Expression, then Statement2 is executed

c. This will continue down to the last ExpressionX Here is an example:

Module Exercise

Public Function Main() As Integer Dim Answer As Byte

Answer = CByte(InputBox( _

"One of the following is not a Visual Basic keyword" & vbCrLf & "1) Function" & vbCrLf &

"2) Except" & vbCrLf & "3) ByRef" & vbCrLf &

"4) Each" & vbCrLf & vbCrLf & "Your Answer? "))

Select Case Answer Case 1

MsgBox("Wrong: Function is a Visual Basic keyword." & vbCrLf & "It is used to create a procedure of a function type") Case 2

MsgBox("Correct: Except is not a keyword in " & vbCrLf & "Visual Basic but __except is a C++ " & vbCrLf & "keyword used in Exception Handling")

Case 3

MsgBox("Wrong: ByRef is a Visual Basic keyword used " & vbCrLf & "to pass an argument by reference to a procedure")

Case 4

MsgBox("Wrong: The ""Each"" keyword is used in " & vbCrLf & "Visual Basic in a type of looping " & vbCrLf & "used to ""scan"" a list of item.")

End Select

Return 0 End Function End Module

The above code supposes that one of the cases will match the value of the Expression factor. This is not always so. If you anticipate that there could be no match between the Expression and one of the Expressions, you can use a Case Else statement at the end of the list. The statement would then look like this:

Select Case Expression Case Expression1 Statement1 Case Expression2 Statement2 Case Expressionk Statementk Case Else Statementk End Select

In this case, the statement after the Case Else will execute if none of the previous expressions matches the Expression factor. Here is an example:

Module Exercise

Public Function Main() As Integer Dim Answer As Byte

Answer = CByte(InputBox( _

"One of the following is not a Visual Basic keyword" & vbCrLf & "1) Function" & vbCrLf &

"2) Except" & vbCrLf & "3) ByRef" & vbCrLf &

"4) Each" & vbCrLf & vbCrLf & "Your Answer? "))

Select Case Answer Case 1

MsgBox("Wrong: Function is a Visual Basic keyword." & vbCrLf & "It is used to create a procedure of a function type") Case 2

MsgBox("Correct: Except is not a keyword in " & vbCrLf & "Visual Basic but __except is a C++ " & vbCrLf & "keyword used in Exception Handling")

Case 3

MsgBox("Wrong: ByRef is a Visual Basic keyword used " & vbCrLf & "to pass an argument by reference to a procedure")

Case 4

MsgBox("Wrong: The ""Each"" keyword is used in " & vbCrLf & "Visual Basic in a type of looping " & vbCrLf & "used to ""scan"" a list of item.")

Case Else MsgBox("Invalid Selection") End Select Return 0 End Function End Module

1. Start Microsoft Visual Basic and create a Console Application named BCR4 2. In the Solution Explorer, right-click Module1.vb and click Rename

3. Type BethesdaCarRental.vb and press Enter 4. Accept to change the name of the module

5. To use a Select...Case condition, change the document as follows: Module BethesdaCarRental

Private Function GetEmployeeName(ByVal EmplNbr As Long) As String Dim Name As String

If EmplNbr = 22804 Then Name = "Helene Mukoko" ElseIf EmplNbr = 92746 Then Name = "Raymond Kouma" ElseIf EmplNbr = 54080 Then Name = "Henry Larson" ElseIf EmplNbr = 86285 Then Name = "Gertrude Monay" Else

Name = "Unknown" End If

Return Name End Function

Public Function Main() As Integer

Dim EmployeeNumber As Long, EmployeeName As String Dim CustomerName As String

Dim TagNumber As String, CarSelected As String Dim CarStatus As Integer, CarCondition As String

Dim Tank As Integer, TankLevel As String Dim RentStartDate As Date, RentEndDate As Date Dim NumberOfDays As Integer

Dim RateType As String, RateApplied As Double Dim OrderTotal As Double

Dim OrderInvoice As String RateType = "Weekly Rate" RateApplied = 0

OrderTotal = RateApplied

EmployeeNumber = CLng(InputBox("Employee number (who processed this order):",

"Bethesda Car Rental", "00000")) EmployeeName = GetEmployeeName(EmployeeNumber)

CustomerName = InputBox("Enter Customer Name:",

"Bethesda Car Rental", "John Doe") TagNumber = InputBox("Enter the tag number of the car to rent:", "Bethesda Car Rental", "000000") CarSelected = Microsoft.VisualBasic.Switch(

TagNumber = "297419", "BMW 335i",

TagNumber = "485M270", "Chevrolet Avalanche", TagNumber = "247597", "Honda Accord LX", TagNumber = "924095", "Mazda Miata", TagNumber = "772475", "Chevrolet Aveo", TagNumber = "M931429", "Ford E150XL", TagNumber = "240759", "Buick Lacrosse", True, "Unidentified Car")

CarStatus = CInt(InputBox("After inpecting it, enter car condition:" & vbCrLf &

"1. Excellent - No scratch, no damage, no concern" & vbCrLf &

"2. Good - Some concerns (scratches or missing something)." &

In document Microsoft Visual Basic 2010 Tutorial (Page 198-200)