As you may recall on Page 9, Visual Basic can test values and return something depending on the result. Another form of this selection process is the Select Case structure. This allows the programmer to choose certain conditions depending on the value of a user input (text box, etc.) or of a variable. In this exercise, we will design a tax withholdings calculator as seen above to demonstrate this method.
Open a new project and name the Project Cases with the project's Name property.
Select the project icon in the Project Explorer window, then type the name in the properties window.
Now to code the application:
txtIncome.Text = ""
lblTotal.Caption = ""
The code above simply declares form-level variables named lngIncome and lngWithholding as the Long.
The lng prefix to Income is just an identifier to remind the programmer what type of variable they are using (in this case Long). We did not do that previously, but it is not necessary. Any serious programmer heavy into programming principles would debate that passionately, but I just want to try to teach you how to do it. I know plenty of people who do not care about most of the principals of programming. The principles will only become partially important when and if you decide to do this for a company. Now we will continue.
lngIncome = txtIncome.Text 'Assigns txtIncome.Text (the text in
textbox txtIncome) to the variable lngIncome
Now we will begin our Case statement.
Select Case lngIncome 'Opens the selection process using the
variable lngIncome. Tests the variable for all the following cases.
Case 0 To 4000 'Sets the range of values for lngIncome; in this case,
0 to 4000
lngWithholding = txtIncome.Text * 0 'No tax; lngWithholding will
equal 0.
End Select 'Ends the select case statements
lblTotal.Caption = Format(lngIncome, "Currency") 'Formats resulting value
for lngIncome in dollars and cents
This code will not tax anything equal to or below 4000 dollars. Refer to the comments to understand code if necessary.
Case 4001 To 15000
'Sets the range of values for lngIncome; in this case, 4001 to
15000.
lngWithholding = txtIncome.Text * .04
This code multiplies the text in txtIncome by .04, then assigns the value to the variable lngWithholding.
Now to finish this program off. Just remember that the Case statement that sets the range determines the decimal, or percentage, that txtIncome.Text will be multiplied by.
Case 15001 To 39999
lngWithholding = lngIncome * .06
Case 40000 To 59999
lngWithholding = lngIncome * 0.1
Case 60000 To 79999
lngWithholding = lngIncome * 0.14
Case 80000 To 99999
lngWithholding = lngIncome * 0.2
Case 100000 To 129999
lngWithholding = lngIncome * 0.27
Case Is >= 130000 'Just says when Case Is greater than or
equal to 130000, multiply as follows
lngWithholding = lngIncome * 0.3
Try testing the application now if you would like.
I hope you enjoyed this study in cases. Now it is time to move on to Page 13.
How do you like this tutorial? Are you learning this language easily? Please let me know via e-mail at webtechjava@yahoo.com.