Conditional Selections |
|
The Select...Case Statement
Introduction
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 stated condition. This is done with the Select Case operator.
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 or an expression that will be checked.
Boolean Case Selections
The value of expression can be a Boolean value. In this case, one case would be True and the other would be False.
The expression can produce a character or a string (a String type). In this case, each Case would deal with a value in double-quotes.
Finding a Character or a Sub-String in a String
The expression can be a natural number from a Byte, an Integer, or a Long type.
Practical Learning: Introducing Case Selection
Private Sub cmdCalculate_Click() Dim incomeTax As Double Dim hourlySalary As Double, timeWorked As Double Dim grossSalary As Double, netPay As Double Dim payrollFrequency As Integer hourlySalary = CDbl(Nz(txtHourlySalary)) timeWorked = CDbl(Nz(txtTimeWorked)) grossSalary = hourlySalary * timeWorked payrollFrequency = CInt(InputBox("Enter a number for the frequency by which " & vbCrLf & _ "the payroll is processed:" & vbCrLf & _ "1 - Weekly" & vbCrLf & _ "2 - Biweekly" & vbCrLf & _ "3 - Semimonthly" & vbCrLf & _ "4 - Monthly", _ "Payroll Frequency", "1")) If (payrollFrequency < 1) Or (payrollFrequency > 4) Then payrollFrequency = 1 Select Case payrollFrequency Case 1 incomeTax = 99.1 + (grossSalary * 0.25) Case 2 incomeTax = 35.5 + (grossSalary * 0.15) Case 3 incomeTax = 38.4 + (grossSalary * 0.15) Case 4 incomeTax = 76.8 + (grossSalary * 0.15) End Select netPay = grossSalary - incomeTax TxtHourlySalary = TxtHourlySalary txtTimeWorked = txtTimeWorked txtGrossSalary = FormatNumber(grossSalary) Select Case payrollFrequency Case 1 txtPayrollFrequency = "Weekly" Case 2 txtPayrollFrequency = "Biweekly" Case 3 txtPayrollFrequency = "Semimonthly" Case 4 txtPayrollFrequency = "Monthly" End Select txtIncomeTax = FormatNumber(incomeTax) txtNetPay = FormatNumber(netPay) End Sub
An Enumerated Case Selection
The expression can be an enumeration. In this case, the expression would be the name of the enumeration. Each Case would deal with one of the members of the enumeration.
The Character Cases of a String
What Case Else?
If you anticipate that there could be no match between the expression and one of the values in the cases, 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 expression-n
statement-n
Case Else
statement-else
End Select
In this case, the statement after the Case Else will execute if none of the values of the cases matches the expression.
Practical Learning: Introducing Case Selection
Private Sub cmdFind_Click() Dim chemicalSymbol As String Dim atomicNumber As Integer Dim elementName As String Dim atomicMass As Single atomicNumber = CInt(Nz(txtAtomicNumber)) Select Case atomicNumber Case 1 chemicalSymbol = "H" elementName = "Hydrogen" atomicMass = 1.0079 Case 2 chemicalSymbol = "He" elementName = "Helium" atomicMass = 4.002682 Case 3 chemicalSymbol = "Li" elementName = "Lithium" atomicMass = 6.941 Case 4 chemicalSymbol = "Be" elementName = "Berylium" atomicMass = 9.0122 Case 5 chemicalSymbol = "B" elementName = "Boron" atomicMass = 10.811 Case 6 chemicalSymbol = "C" elementName = "Carbon" atomicMass = "12.01074" Case Else chemicalSymbol = "N/A" elementName = "Unknown" atomicMass = 0# End Select txtChemicalSymbol = CStr(chemicalSymbol) txtElementName = CStr(elementName) txtAtomicMass = CStr(atomicMass) End Sub
Private Sub cmdCalculate_Click() Dim incomeTax As Double Dim hourlySalary As Double, timeWorked As Double Dim grossSalary As Double, netPay As Double Dim payrollFrequency As Integer hourlySalary = CDbl(Nz(TxtHourlySalary)) timeWorked = CDbl(Nz(txtTimeWorked)) grossSalary = hourlySalary * timeWorked payrollFrequency = CInt(InputBox("Enter a number for the frequency by which " & vbCrLf & _ "the payroll is processed:" & vbCrLf & _ "1 - Weekly" & vbCrLf & _ "2 - Biweekly" & vbCrLf & _ "3 - Semimonthly" & vbCrLf & _ "4 - Monthly", _ "Payroll Frequency", "1")) If (payrollFrequency < 1) Or (payrollFrequency > 4) Then payrollFrequency = 1 Select Case payrollFrequency Case 1 incomeTax = 35.5 + (grossSalary * 0.15) Case 2 incomeTax = 38.4 + (grossSalary * 0.15) Case 3 incomeTax = 76.8 + (grossSalary * 0.15) Case Else incomeTax = 99.1 + (grossSalary * 0.25) End Select netPay = grossSalary - incomeTax TxtHourlySalary = TxtHourlySalary txtTimeWorked = txtTimeWorked txtGrossSalary = FormatNumber(grossSalary) Select Case payrollFrequency Case 1 txtPayrollFrequency = "Biweekly" Case 2 txtPayrollFrequency = "Semimonthly" Case 3 txtPayrollFrequency = "Monthly" Case Else txtPayrollFrequency = "Weekly" End Select txtIncomeTax = FormatNumber(incomeTax) txtNetPay = FormatNumber(netPay) End Sub
Combining Cases
When creating a Select Case situation, you may end up using two or more cases that proceed to the same outcome. Here is an example:
Private Sub cmdFunction_Click() Dim Gender As String Gender = "f" Select Case Gender Case "f" MsgBox("Female") Case "female" MsgBox("Female") Case "m" MsgBox("Male") Case "male" MsgBox("Male") Case Else MsgBox("Unknown") End Select End Sub
Instead of using one value for a case, you can apply more than one. To do this, on the right side of the Case keyword, you can separate the expressions with commas. Here are examples:
Private Sub cmdFunction_Click() Dim Gender As String Gender = "F" Select Case Gender Case "f", "female" MsgBox("Female") Case "m", "male" MsgBox("Male") Case Else MsgBox("Unknown") End Select End Sub
Applying a Range of Values for a Case
You can use a range of values for a case. To do this, on the right side of Case, enter the lower value, followed by To, followed by the higher value. Here is an example:
<script runat="server"> Sub Find() Dim nbr As Integer nbr = 24 Select Case nbr Case 0 To 17 Response.Write("Teen") Case 18 To 55 Response.Write("Adult") Case Else Response.Write("Senior") End Select End Sub End Sub </script>
Validating a Range of Cases
You can use a range of values for a case. To do this, on the right side of a Case, enter the lower value, followed by To, followed by the higher value. Here is an example:
Private Sub cmdFunction_Click() Dim Age As Integer Age = 24 Select Case Age Case 0 To 17 MsgBox("Teen") Case 18 To 55 MsgBox("Adult") Case Else MsgBox("Senior") End Select End Sub
Consider the following procedure:
Private Sub cmdFunction_Click() Dim Number As Integer Number = 448 Select Case Number Case -602 MsgBox "-602" Case 24 MsgBox "24" Case 0 MsgBox "0" End Select End Sub
Obviously, this Select Case statement will work in rare cases only when the expression of a case exactly matches the value sought for. In reality, for this type of scenario, you could validate a range of values. The Visual Basic language provides an alternative. You can check whether the value of the Expression responds to a criterion instead of an exact value. To create it, you use the Is operator with the following formula:
Is Operator Value
You start with the Is keyword. It is followed by one of the Boolean operators we know already: =, <>, <, <=, >, or >=. On the right side of the Boolean operator, type the desired value. Here are examples:
Private Sub cmdFunction_Click() Dim Number As Integer Number = -448 Select Case Number Case Is < 0 MsgBox("The number is negative") Case Is > 0 MsgBox("The number is positive") Case Else MsgBox("0") End Select End Sub
Although we used a natural number here, you can use any appropriate logical comparison that can produce a True or a False result. You can also combine it with the other alternatives we saw previously, such as separating the expressions of a case with commas.
Select...Case and Conditional Built-In Functions
To functionaly apply select cases, the Visual Basic language provides a function named Choose that can check a condition and take an action. The Choose() function is presented as follows:
Public Function Choose( _ ByVal Index As Double, _ ByVal ParamArray Choice() As Variant _ ) As Variant
This function takes two required arguments. The first argument is equivalent to the expression of our Select Case formula. The first argument must be a number (a Byte, an Integer, a Long, a Single, or a Double value). In place of the Case sections, for the second argument, provide the equivalent expression-x as a list of values. The values are separated by commas. Here is an example:
Choose(payrollFrequency, "Weekly", "Biweekly", "Semimonthly", "Monthly")
As mentioned already, the values of the second argument are provided as a list. Each member of the list uses an index. The first member of the list, which is the second argument of this function, has an index of 1. The second value of the argument, which would be the third argument of the function, has an index of 2. You can continue adding the values of the second argument as you see fit.
When the Choose() function has been called, it returns a value of type Variant. You can retrieve that value, store it in a variable and use it as you see fit.
The value of the Choose() function can be expressions. Here is an example:
Private Sub cmdCalculate_Click()
Dim incomeTax As Double
Dim hourlySalary As Double, timeWorked As Double
Dim grossSalary As Double, netPay As Double
Dim payrollFrequency As Integer
hourlySalary = CDbl(Nz(TxtHourlySalary))
timeWorked = CDbl(Nz(txtTimeWorked))
grossSalary = hourlySalary * timeWorked
payrollFrequency = CInt(InputBox("Enter a number for the frequency by which " & vbCrLf & _
"the payroll is processed:" & vbCrLf & _
"1 - Weekly" & vbCrLf & _
"2 - Biweekly" & vbCrLf & _
"3 - Semimonthly" & vbCrLf & _
"4 - Monthly", _
"Payroll Frequency", "1"))
If (payrollFrequency < 1) Or (payrollFrequency > 4) Then payrollFrequency = 1
incomeTax = Choose(payrollFrequency, 99.1 + (grossSalary * 0.25), 35.5 + (grossSalary * 0.15), 38.4 + (grossSalary * 0.15), incomeTax = 76.8 + (grossSalary * 0.15))
netPay = grossSalary - incomeTax
TxtHourlySalary = TxtHourlySalary
txtTimeWorked = txtTimeWorked
txtGrossSalary = FormatNumber(grossSalary)
txtPayrollFrequency = Choose(payrollFrequency, "Weekly", "Biweekly", "Semimonthly", "Monthly")
txtIncomeTax = FormatNumber(incomeTax)
txtNetPay = FormatNumber(netPay)
End Sub
Select...Case-Related Functions: Switch()
As another alternative to an If...Then condition, the Visual Basic language provides a function named Switch. Its syntax is:
Public Function Switch( _ ByVal ParamArray VarExpr() As Variant _ ) As Variant
This function takes one required argument. To use it in an If...Then scenario, pass the argument as follows:
Switch(ConditionToCheck, Statement)
In the ConditionToCheck placeholder, pass a Boolean expression that can be evaluated to True or False. If that condition is true, the second argument would be executed.
When the Switch() function has been called, it produces a value of type Variant (such as a string) that you can use as you see fit. For example, you can store it in a variable. Here is an example:
Private Sub cmdFunction_Click()
Dim Status As Integer, EmploymentStatus As String
Status = 1
EmploymentStatus = "Unknown"
EmploymentStatus = Switch(Status = 1, "Full Time")
MsgBox "Employment Status: " & EmploymentStatus
End Sub
In this example, we used a number as argument. You can also use another type of value, such as an enumeration. When using the Switch function, if you call it with a value that is not checked by the first argument, the function produces an error. To apply this function to an If...Then...Else scenario, you can call it using the following formula:
Switch(Condition1ToCheck, Statement1, Condition2ToCheck, Statement2)
In the Condition1ToCheck placeholder, pass a Boolean expression that can be evaluated to True or False. If that condition is true, the second argument would be executed. To provide an alternative to the first condition, pass another condition as Condition2ToCheck. If the Condition2ToCheck is true, then Statement2 would be executed. Once gain, remember that you can get the value returned by the Switch() function and use it.
Nesting a Conditional Statement in a Case Selection
A conditional statement can be created in the body of a Case statement, which is referred to as nesting. In fact, a Select statement can be created in a Case or a Case Else section.
A conditional statement can be created or nested in a case of a conditional selection.
Learning: Nesting a Conditional Statement in a Case Selection
Private Sub cmdCalculate_Click() Dim salary As Double Dim exemptions As Double Dim allowanceRate As Double Dim maritalStatus As Integer Dim withheldAmount As Double Dim taxableGrossWages As Double Dim withholdingAllowances As Double allowanceRate = 76.9 salary = CDbl(txtSalary) exemptions = CDbl(txtExemptions) withholdingAllowances = allowanceRate * exemptions taxableGrossWages = salary - withholdingAllowances maritalStatus = CInt(InputBox("Enter the employee's marital status:" & vbCrLf & _ "1 for Single" & vbCrLf & _ "2 for Married" & vbCrLf & _ "3 for Separated" & vbCrLf & _ "4 for Widow", _ "Marital Status", "1")) Select Case maritalStatus Case 1 If taxableGrossWages <= 44# Then withheldAmount = 0# ElseIf (taxableGrossWages > 44#) And (taxableGrossWages <= 222#) Then withheldAmount = (taxableGrossWages - 44#) * 10# / 100# ElseIf (taxableGrossWages > 222#) And (taxableGrossWages <= 764#) Then withheldAmount = 17.8 + ((taxableGrossWages - 222#) * 15# / 100#) ElseIf (taxableGrossWages > 764#) And (taxableGrossWages <= 1789#) Then withheldAmount = 99.1 + ((taxableGrossWages - 764#) * 25# / 100#) ElseIf (taxableGrossWages > 1789#) And (taxableGrossWages <= 3685#) Then withheldAmount = 355.05 + ((taxableGrossWages - 1789#) * 28# / 100#) ElseIf (taxableGrossWages > 3685#) And (taxableGrossWages <= 7958#) Then withheldAmount = 886.23 + ((taxableGrossWages - 3685#) * 33# / 100#) ElseIf (taxableGrossWages > 7958#) And (taxableGrossWages <= 7990#) Then withheldAmount = 2296.32 + ((taxableGrossWages - 7958#) * 35# / 100#) Else withheldAmount = 2307.52 + ((taxableGrossWages - 7990#) * 39.6 / 100#) End If Case 2 If taxableGrossWages <= 165# Then withheldAmount = 0# ElseIf (taxableGrossWages > 165#) And (taxableGrossWages <= 520#) Then withheldAmount = (taxableGrossWages - 165#) * 10# / 100# ElseIf (taxableGrossWages > 520#) And (taxableGrossWages <= 1606#) Then withheldAmount = 35.5 + ((taxableGrossWages - 520#) * 15# / 100#) ElseIf (taxableGrossWages > 1606#) And (taxableGrossWages <= 3073#) Then withheldAmount = 198.4 + ((taxableGrossWages - 1606#) * 25# / 100#) ElseIf (taxableGrossWages > 3073#) And (taxableGrossWages <= 4597#) Then withheldAmount = 565.15 + ((taxableGrossWages - 3073#) * 28# / 100#) ElseIf (taxableGrossWages > 4597#) And (taxableGrossWages <= 8079#) Then withheldAmount = 991.87 + ((taxableGrossWages - 4597#) * 33# / 100#) ElseIf (taxableGrossWages > 8079#) And (taxableGrossWages <= 9105#) Then withheldAmount = 2140.93 + ((taxableGrossWages - 8079#) * 35# / 100#) Else withheldAmount = 2500.03 + ((taxableGrossWages - 9105#) * 39.6 / 100#) End If Case Else withheldAmount = 0# End Select txtMaritalStatus = Choose(maritalStatus, "Single", "Married", "Separated", "Widow") txtAllowances = FormatNumber(withholdingAllowances) txtTaxableGrossWages = FormatNumber(taxableGrossWages) txtFederalIncomeTax = FormatNumber(withheldAmount) End Sub
Public Enum MaritalStatus StatusSingle = 1 StatusMarried = 2 StatusSeparated = 3 StatusWidow = 4 End Enum Public Enum PayrollFrequency Weekly = 1 Biweekly = 2 End Enum Private Sub cmdCalculate_Click() Dim salary As Double Dim exemptions As Double Dim allowanceRate As Double Dim withheldAmount As Double Dim taxableGrossWages As Double Dim marriedStatus As MaritalStatus Dim withholdingAllowances As Double Dim payFrequency As PayrollFrequency allowanceRate = 76.9 salary = CDbl(txtSalary) exemptions = CDbl(txtExemptions) withholdingAllowances = allowanceRate * exemptions taxableGrossWages = salary - withholdingAllowances marriedStatus = CInt(InputBox("Enter the employee's marital status:" & vbCrLf & _ "1 for Single" & vbCrLf & _ "2 for Married" & vbCrLf & _ "3 for Separated" & vbCrLf & _ "4 for Widow", _ "Marital Status", "1")) payFrequency = CInt(InputBox("Enter the frequency by which the payroll is processed:" & vbCrLf & _ "1 - Weekly" & vbCrLf & _ "2 - Biweekly", _ "Payroll Frequency", "1")) Select Case payFrequency Case PayrollFrequency.Weekly Select Case marriedStatus Case MaritalStatus.StatusSingle If taxableGrossWages <= 44# Then withheldAmount = 0# ElseIf (taxableGrossWages > 44#) And (taxableGrossWages <= 222#) Then withheldAmount = (taxableGrossWages - 44#) * 10# / 100# ElseIf (taxableGrossWages > 222#) And (taxableGrossWages <= 764#) Then withheldAmount = 17.8 + ((taxableGrossWages - 222#) * 15# / 100#) ElseIf (taxableGrossWages > 764#) And (taxableGrossWages <= 1789#) Then withheldAmount = 99.1 + ((taxableGrossWages - 764#) * 25# / 100#) ElseIf (taxableGrossWages > 1789#) And (taxableGrossWages <= 3685#) Then withheldAmount = 355.05 + ((taxableGrossWages - 1789#) * 28# / 100#) ElseIf (taxableGrossWages > 3685#) And (taxableGrossWages <= 7958#) Then withheldAmount = 886.23 + ((taxableGrossWages - 3685#) * 33# / 100#) ElseIf (taxableGrossWages > 7958#) And (taxableGrossWages <= 7990#) Then withheldAmount = 2296.32 + ((taxableGrossWages - 7958#) * 35# / 100#) Else withheldAmount = 2307.52 + ((taxableGrossWages - 7990#) * 39.6 / 100#) End If Case MaritalStatus.StatusMarried If taxableGrossWages <= 165# Then withheldAmount = 0# ElseIf (taxableGrossWages > 165#) And (taxableGrossWages <= 520#) Then withheldAmount = (taxableGrossWages - 165#) * 10# / 100# ElseIf (taxableGrossWages > 520#) And (taxableGrossWages <= 1606#) Then withheldAmount = 35.5 + ((taxableGrossWages - 520#) * 15# / 100#) ElseIf (taxableGrossWages > 1606#) And (taxableGrossWages <= 3073#) Then withheldAmount = 198.4 + ((taxableGrossWages - 1606#) * 25# / 100#) ElseIf (taxableGrossWages > 3073#) And (taxableGrossWages <= 4597#) Then withheldAmount = 565.15 + ((taxableGrossWages - 3073#) * 28# / 100#) ElseIf (taxableGrossWages > 4597#) And (taxableGrossWages <= 8079#) Then withheldAmount = 991.87 + ((taxableGrossWages - 4597#) * 33# / 100#) ElseIf (taxableGrossWages > 8079#) And (taxableGrossWages <= 9105#) Then withheldAmount = 2140.93 + ((taxableGrossWages - 8079#) * 35# / 100#) Else withheldAmount = 2500.03 + ((taxableGrossWages - 9105#) * 39.6 / 100#) End If End Select Case PayrollFrequency.Biweekly Select Case marriedStatus Case MaritalStatus.StatusSingle If taxableGrossWages <= 88# Then withheldAmount = 0# ElseIf (taxableGrossWages > 88#) And (taxableGrossWages <= 443#) Then withheldAmount = (taxableGrossWages - 88#) * 10# / 100# ElseIf (taxableGrossWages > 443#) And (taxableGrossWages <= 1529#) Then withheldAmount = 35.5 + ((taxableGrossWages - 443#) * 15# / 100#) ElseIf (taxableGrossWages > 1529#) And (taxableGrossWages <= 3579#) Then withheldAmount = 198.4 + ((taxableGrossWages - 1529#) * 25# / 100#) ElseIf (taxableGrossWages > 3579#) And (taxableGrossWages <= 7369#) Then withheldAmount = 710.9 + ((taxableGrossWages - 3579#) * 28# / 100#) ElseIf (taxableGrossWages > 7369#) And (taxableGrossWages <= 15915#) Then withheldAmount = 1772.1 + ((taxableGrossWages - 7369#) * 33# / 100#) ElseIf (taxableGrossWages > 15915#) And (taxableGrossWages <= 15981#) Then withheldAmount = 4592.28 + ((taxableGrossWages - 15915#) * 35# / 100#) Else withheldAmount = 4615.38 + ((taxableGrossWages - 15981#) * 39.6 / 100#) End If Case MaritalStatus.StatusMarried If taxableGrossWages <= 331# Then withheldAmount = 0# ElseIf (taxableGrossWages > 331#) And (taxableGrossWages <= 1040#) Then withheldAmount = (taxableGrossWages - 331#) * 10# / 100# ElseIf (taxableGrossWages > 1040#) And (taxableGrossWages <= 3212#) Then withheldAmount = 70.9 + ((taxableGrossWages - 1040#) * 15# / 100#) ElseIf (taxableGrossWages > 3212#) And (taxableGrossWages <= 6146#) Then withheldAmount = 396.7 + ((taxableGrossWages - 3212#) * 25# / 100#) ElseIf (taxableGrossWages > 6146#) And (taxableGrossWages <= 9194#) Then withheldAmount = 1130.2 + ((taxableGrossWages - 6146#) * 28# / 100#) ElseIf (taxableGrossWages > 9194#) And (taxableGrossWages <= 16158#) Then withheldAmount = 1983.64 + ((taxableGrossWages - 9194#) * 33# / 100#) ElseIf (taxableGrossWages > 16158#) And (taxableGrossWages <= 18210#) Then withheldAmount = 4281.76 + ((taxableGrossWages - 16158#) * 35# / 100#) Else withheldAmount = 4999.96 + ((taxableGrossWages - 18210#) * 39.6 / 100#) End If End Select End Select txtMaritalStatus = Choose(marriedStatus, "Single", "Married", "Separated", "Widow") txtAllowances = FormatNumber(withholdingAllowances) txtTaxableGrossWages = FormatNumber(taxableGrossWages) txtFederalIncomeTax = FormatNumber(withheldAmount) End Sub
|
||
Previous | Copyright © 2002-2022, FunctionX, Inc. | Next |
|