Trying to learn VB.NET, I have a question about the following code:

Public Class WageCalculatorForm
    'handles Click event
    Private Sub calculateButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles calculateButton.Click

        'declare variables
        Dim hours As Double
        Dim wage As Decimal
        Dim earnings As Decimal

        Const HOUR_LIMIT As Integer = 40 'declare constant

        'assign values from user input
        hours = Val(hoursTextBox.Text)
        wage = Val(wageTextBox.Text)

        'determine wage amount
        If hours <= HOUR_LIMIT Then
            'if under or equal to 40 hours, regular wages
            earnings = hours * wage
        Else
            'if over 40 hours, regular wages for first 40
            earnings = HOUR_LIMIT * wage

            'time and a half for the additional hours
            earnings += (hours - HOUR_LIMIT) * (1.5 * wage)
        End If

        'assign the result to its corresponding Label
        earningsResultLabel.Text = earnings

    End Sub
End Class ' WageCalculatorForm

Why couldn't

earningsResultLabel.Text = earnings

be put under

'assign values from user input
        hours = Val(hoursTextBox.Text)
        wage = Val(wageTextBox.Text)

as

earnings = Val(earningsResultLabel.Text)

?

Recommended Answers

All 3 Replies

earningsResultLabel.Text holds an empty string which Val cannot convert into an integer ?

Because you havn't calculated it yet. And would always be zero(0). Leave it as it is if you want a correct answer.

Okay, that makes sense. Thanks waynespangler!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.