Let me start by saying that this exercise (p. 290) was far from pointless, even if the overall program is a complete waste of time. While you could never use a program that makes
assumptions like, 8 hours sleep per night, 360 days per year and 30 days per month, you can learn from actually doing the work to set it up.
I learned how to keep an answer down to 2 decimal places. And the equation to work out the details entered in to the program was "fun" but like I said, it is hardly a program that can be used outside of being a learning tool -just ask anyone who has a birthday on the 31st of July for example.
We will also have to learn how to limit the number a person can enter in to a text field. And by that I mean a number numerically, not a number of digits. Unless of course you were one of those lucky people who has a birthday on the 85th of the 17th ;-). What's more, the whole equation could be worked out a lot easier with an if/else statement.
But I digress... So I came up with the following interface design:
I felt this was pretty close to what the description was after. It uses
Jokerman regular 20pt for the main heading and CaliforniaFB for the other labels in various sizes. As with all of the other programs we worked on in this chapter, the calculate button does the major share of the work.
As you can see, the user enters the current date and then their birth date and presses the calculate button. The calculate click event then takes all of the information and runs its equation in the background before popping up a label with the result.
When I first did this, I used the decimal data type and it gave me one long number that was really hard to read. So after the ToString function, I gave it a forced literal of
number and it comma separated the number, making it easier to read but also giving it 2 decimal places.
I couldn't use an integer because it would still make the result hard to read, so I had to use the number literal. What I did to get around the problem was was use ("N0") after the ToString function.
My code for this part looked like this:
lblTotalHoursSlept.Text = strFirstName.ToString & ", you have slept for a total of " & decTotalHoursSleep.ToString("N0") & " hours"
That took the label and displayed the result with comma separated thousands, minus the decimal places. That is N + zero by the way, not No.
The equation worked out a little differently. I spent the afternoon trying to come up with the best way to attack this problem, and in the end, I subtracted the number in the birth date field from the number in the corresponding current date field and then multiplied the result by the number we were given in the exercise. The numbers we were given (360 days for a year, 30 day for a month) were stored as constants.
My code to work out the equation looked like this:
decTotalHoursSleep = (((intDateYear - intBirthYear) * _cintDaysInYear) _
+ ((intDateMonth - intBirthMonth) * _cintDaysInMonth) _
+ (intDateDay - intBirthDay)) * _cintHoursSleepPerDay
and the constants were declared in the public class like this:
Const _cintHoursSleepPerDay As Integer = 8
Const _cintDaysInYear As Integer = 360
Const _cintDaysInMonth As Integer = 30
You will notice that the entire equation is bracketed so that it is worked out before multiplying it by the hours slept (8).
If anyone else came up with a more efficient way of doing this (under the directions given in the exercise of course), I'd love it if you could leave a comment.
Have a good one,
Tim.