So I'm new to programming and currently, I'm creating a school project with Visual Basic and .NET, I know that I might either have a problem with formatting or converting or assigning a value(which I don't know how to do) but whatever I try the multiplied result always gives me 0, (Salarybox is the "RichTextBox" I'm using to put the result in)

Private Sub Guna2Button1_Click(sender As Object, e As EventArgs) Handles Guna2Button1.Click
        Dim DailyPay As Integer


        If empposition.Text = "" Then
            MsgBox("Select an Employee")
        ElseIf wdays.Text = "" Or Integer.Parse(wdays.Text) > 28 Then
            MsgBox("Enter a valid Number of Days")
        Else
            If empposition.Text = "Receptionist" Then
                DailyPay = 1200
            ElseIf empposition.Text = "Reception manager" Then
                DailyPay = 1500
            ElseIf empposition.Text = "House keeping manager" Then
                DailyPay = 700
            ElseIf empposition.Text = "Bar manager" Then
                DailyPay = 1500
            ElseIf empposition.Text = "Barman" Then
                DailyPay = 850
            ElseIf empposition.Text = "Waiter" Then
                DailyPay = 550
            ElseIf empposition.Text = "Vendor" Then
                DailyPay = 800
            ElseIf empposition.Text = "Shop manager" Then
                DailyPay = 1500
            ElseIf empposition.Text = "IT manager" Then
                DailyPay = 1500
            ElseIf empposition.Text = "Technicien" Then
                DailyPay = 1000
            ElseIf empposition.Text = "Spa manager" Then
                DailyPay = 1200
            ElseIf empposition.Text = "Beautician" Then
                DailyPay = 800
            ElseIf empposition.Text = "Security man" Then
                DailyPay = 500
            End If
            Dim total As Integer = DailyPay * Integer.Parse(wdays.Text)
            Salarybox.Text = "Employee ID:   " & empid.Text & vbLf & "Employee Name:   " & ename.Text & vbLf & "Employee LastName:   " & lastname.Text & vbLf & "Position:   " & empposition.Text & vbLf & "Worked Days:   " & wdays.Text & vbLf & "Daily Salary:   " & DailyPay & vbLf & "Total Amount:   " & total

        End If
    End Sub

Dani AI

Generated

The debugging advice from and is the right first step. Complement that by validating inputs, avoiding fragile string-matching for pay rates, and using safer parsing and types so a missed match cannot silently produce zero.

Key checks and fixes to apply

  • Validate the days field with Integer.TryParse (and Trim) and enforce a sensible range before doing any math.
  • Avoid relying on visible text for business logic. If the job list is a ComboBox or data-bound control, use SelectedValue/an ID instead of the displayed label, or normalize the string with Trim() and a case-insensitive comparison.
  • Provide a clear fallback: if a position lookup fails, show an error (or log) instead of letting the rate remain the default zero.
  • Use Decimal for money calculations to avoid rounding/overflow issues and format the output with currency formatting when writing to the result box.
  • Turn on Option Strict On and Option Explicit On to catch implicit conversions and accidental shadowing at compile time.

Minimal VB pattern to follow

Dim days As Integer
If Not Integer.TryParse(wdays.Text.Trim(), days) OrElse days <= 0 Then
    MessageBox.Show("Enter valid days")
    Return
End If

Dim rate = GetRateForPosition(empposition.Text) ' returns Nullable(Of Decimal)
If Not rate.HasValue Then
    MessageBox.Show("Unknown position")
    Return
End If

Dim total As Decimal = rate.Value * days
Salarybox.Text = total.ToString("C0")

Debugging tips that help quickly

  • Set a breakpoint before the calculation, Add Watch for the position text and parsed days, and evaluate empposition.Text.Trim() in the Immediate window.
  • Check for variable shadowing (local vs. field named the same) and for data-binding differences that make Text not equal to the displayed label.

Recommended Answers

All 8 Replies

Have you tried setting a breakpoint in your handler and stepping through the code?

commented: No I didn't because it didn't get mentioned in any of the solutions I found, If It's necessary, in which line of the code I should set the breakpoint +0

As to where to break, line 40 because I can mouse over every variable and string to see what could be wrong.

commented: Okay I did it, but what comes after? +0

Sorry but I should have written "YOU can mouse every variable and string to see what is wrong."

This is classic debugging skill. It can be hard the first time but gets easier.

commented: Okay, so I did that and it says that in Integer.Parse(wdays.Text):ArgumentNullException/FormatingException/OverflowException +0

thanks for sharing awesome knowledge . I get same knowledge by other forums platform. thanks again

What does wdays.Text contain?

commented: it's the textbox that I'm writing the number of days in. (Integers) +0

So you found DailyPay is zero. While at the breakpoint find out why by moving the mouse point over empposition.Text to see if you ever had a match. Tip: I would have added one more else to cover where there was no match for the empposition.

Remember this isn't the only issue I see in said code. For example, numbers may need to be converted to text strings before being used for Salary.Text. I won't have this version of VB.net so I leave that for you to explore.

commented: Thank you, well I did that and it returned the last else value which means the problem is in the if else statement,but I still can't see what it is +0

Remember I'm not there to see what you see and if I do help you find ONE issue, there can be more.

-> empposition.Text should be a text box's text. Since DailyPay is zero my bet is the text never matched in all those if's. Only you can examine this area.

commented: Exactly, turned out the text is not matching the text in "If statements" cuz of some added space, Thank you +0
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.