Before we can design our application I need to build on the concepts presented in lesson four around casting and conversions. In simple terms every application that accepts user input is at risk of throwing an exception (very bad thing...) and this basically make the application crash and you get a stop pay put on your big paycheck.
In our program we will accept two values (hours worked and hourly wage) and then we will perform a calculation on those variables and display your gross earnings in currency based on standard and overtime hours worked. Overtime hours are calculated at a rate of 1.5 times your standard hourly rate.
When we accept the input for hours worked and wage rate it might not be in a format that is acceptable to the program. For example we expect a range of values between 1 and probably no more than 70 for hours worked and for hourly wage we also expect a positive number in the range of somewhere between let's say 25 and 100.
No matter the exact numbers. If your user enters 25a instead of 25 your program must know how to deal with this because 25a is not a numeric value and no math can be performed on this. Same goes with the hourly wage rate too.
So, we first need to address the issue of taking the input from the application and converting it to a proper data type. Meaning that even though your user is entering a number in your textbox, it really is a string at this point as far as the program is concerned.
In an earlier lesson we decided to declare the three variables as follows:
Dim hours As Double
Dim wage As Decimal
Dim earnings As Decimal
Based on our variables we will need to convert hours from the text input to a double type and the wage variable to a decimal type and when we display the earnings amount this needs to be in a decimal format as well.
How do we do this? Well, there are many different ways to accomplish this and you will quickly learn in programming there are many ways to accomplish the same thing. If you do this for a living you find out quickly the best way is the way that doesn't produce errors and gets you paid.
You can use four different approaches:
The first way is to use the shared method converting the string input to a double. This won't avoid exception errors, it will just convert the data type for you and possibly round the number.
hours = Convert.ToDecimal(txtHoursWorked.Text)
The second way is to use a function for explicit casting. Just as the shared method won't avoid exception errors, this approach won't either.
hours = CDbl(txtHoursWorked.Text)
The third way is to use the CType() function where you pass the data type as an argument.
hours = CType(txtHoursWorked.Text, Double)
The fourth way is to use the Parse method ToDouble(). This approach converts the string value to the Double data type as you might expect. However, this method only recognizes standard numeric characters and will throw an exception if non-numeric characters are entered by your user into the textbox.
hours = Double.Parse(txtHoursWorked.Text)
Technically any of these approaches could be adopted and made to work in this small and simple program. At this stage of the game you simply need to learn all of them and understand how to apply them in your own work.
Converting the data input from a string to the correct type is only half the battle. You need to perform some type of data validation at a minimum and possiblly code exception handling as well. In this lesson I will show you how to code data validation for the two numeric variables and in a future lesson we will dig deeper into the mechanics of how to code much more detailed exception handling techniques. We will be doing this constantly in future lessons so be careful what you ask for.
We will use a special function IsNumeric() to test if the information entered into our textbox is numeric or not and display a message to the user if it isnt and if so, continue on by assinging a value to the variable with the proper data type.
Here is what that will look like for us.
If Not IsNumeric(txtHoursWorked.Text) Then
MessageBox.Show("Please enter a valid number for hours.", "Entry Error")
Exit Sub ' exits from the event handler
End If
' code to set the value of the hours variable
hours = Convert.ToDouble(txtHoursWorked.Text)
Then all we have to do is perform the math functions and display the result via the earnings variable. I will cover all of that in the video segment of the lesson.
So, that should about wrap up the written part of the lesson so now it is time to go build the user interface and write some code.
The video is in High-Def format so be sure to click the HD button in the lower right corner or you can select the Title to go to YouTube and watch the lesson in full screen.
I hope you found this lesson useful and let me know how you are doing with the lessons.
Lesson 5 - Video Part 1
Lesson 5 - Video Part 2
Tim











