Error in variable calculation?

Yaztrak

New member
Joined
Jan 13, 2012
Messages
2
Programming Experience
Beginner
I'll be blunt. I'm taking a class on VB.NET and I have very little idea what I'm doing in this program, just yet. The problem is in here... somewhere. The assignment is to code a simple form that takes the hours a person's worked and the number of their fictional dependents, mix them up with a few constants, and spit out the person's paycheck, post-tax.

Problem being, I can't seem to get it to actually output the results of any calculations.

Pardon what I think is incredibly clumsy coding. Still wrapping my head around typing, but I guess that could be the root of my problem. Perhaps I should stop guessing, since so far those guesses haven't led to a solution. Anyway-- the output winds up as 0.00 instead of the actual solution to the formula. Any suggestions or explanation would be appreciated.

VB.NET:
 'Constant section Lab #2  CS 115 

        Const FED_TAX As Decimal = CDec(0.2)        '20% Federal Tax 
        Const STATE_TAX As Decimal = CDec(0.032)    '3.2% State Tax 
        Const DEPENDENTS As Decimal = CDec(48.6)    'Deduction for dependents 
        Const HOURLY_RATE As Decimal = 28     'Rate per hour 

        'Declaration section 
        Dim strName As String               'Name of employee 
        Dim decDependents As Decimal           'Number of dependents 
        Dim decHours As Decimal              'Hours worked 
        Dim decGross As Decimal             'Gross pay 
        Dim decFedTax As Decimal            'Federal tax 
        Dim decStateTax As Decimal          'State tax 
        Dim decTax As Decimal               'Total taxes 
        Dim decNetPay As Decimal            'Net pay 
        Dim decFactor As Decimal = 1        'number of paychecks per month 


        If decHours > 40 Then 'Branches formula based on overtime
            'If >40, overtime pay
            decGross = CDec(40 * HOURLY_RATE + (decHours - 40) * HOURLY_RATE * 1.5)
        Else
            decGross = CDec(decHours * HOURLY_RATE)
        End If
        decFedTax = CDec(FED_TAX * (decGross - decDependents * DEPENDENTS))
        decStateTax = CDec(STATE_TAX * decGross)
        decNetPay = CDec(decGross - decFedTax - decStateTax)

txtCalculate.Text = CStr(decNetPay)
 
As far as I can see from the values you are giving it, the output is correct at 0.00. This is because the decHours is 0, because you havent set the value to be anything else.
 
You have to debug your code. What variable values do your expect at at point, and what are they actually at runtime? If you don't know where to start put a break-point on the first statement that evaluates or assigns an expression (If decHours > 40 Then), check the variable values with debugger, mouse hover a variable to see its current value. If that is ok step through the rest of the code until you find out where and why it does not meet your expectations.
Debugging Your Visual Basic Application
 
Yikes. It was indeed simply because I didn't have decHours and decDependents defined. It works perfectly now. I think I may have accidentally edited that part of the code out earlier, and assumed there was some specific codeword I'd missed in class. Probably the silliest mistake I've made yet.

It's not exactly pretty, but it meets the demands of the client so it all works out, I guess. Thanks for the speedy response. I guess everything's all resolved now.
:dunce:
 
Back
Top