-1

I can't understand how I should fix my code. I keep getting this error.

InvalidCastException was unhandled An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Additional information: Conversion from string "" to type 'Double' is not valid.

I'm confused on how to fix this. I don't understand the error. It starts at the beginning of the If. Here is the code I am using:

Public class Income_Tax
Dim rate as Double
Dim difference as Double

Private Sub textboxqitni_TextChanged(sender As Object, e As EventArgs) Handles textboxqitni.TextChanged

    If textboxqitni.Text >= 0 And textboxqitni.Text <= 10000 Then
        textboxittd.Text = textboxqitni.Text * 0.05
    ElseIf textboxqitni.Text >= 10000 And textboxqitni.Text <= 30000 Then
        difference = textboxqitni.Text - 10000
        rate = difference * 0.1
        textboxittd.Text = rate + 500
    ElseIf textboxqitni.Text >= 30000 And textboxqitni.Text <= 70000 Then
        difference = textboxqitni.Text - 30000
        rate = difference * 0.15
        textboxittd.Text = rate + 2500
    ElseIf textboxqitni.Text >= 70000 And textboxqitni.Text <= 140000 Then
        difference = textboxqitni.Text - 70000
        rate = difference * 0.2
        textboxittd.Text = rate + 8500
    ElseIf textboxqitni.Text >= 140000 And textboxqitni.Text <= 250000 Then
        difference = textboxqitni.Text - 140000
        rate = difference * 0.25
        textboxittd.Text = rate + 22500
    ElseIf textboxqitni.Text >= 250000 And textboxqitni.Text <= 500000 Then
        difference = textboxqitni.Text - 250000
        rate = difference * 0.3
        textboxittd.Text = rate + 50000
    ElseIf textboxqitni.Text >= 500000 And textboxqitni.Text <= 999999999999999 Then
        difference = textboxqitni.Text - 500000
        rate = difference * 0.32
        textboxittd.Text = rate + 125000
    End If

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
  • You need to typecast your textbox value to integer / double before comparing it. By default textbox.text will return string value and you can't compare string value directly with int / double value without type casting. – maddy23285 Dec 29 '17 at 11:08

2 Answers2

2

The first thing I suggest is to use a decimal datatype for your calculations.
It seems that your mathematical operations involve monetary values and in this case you should always use a decimal datatype to avoid floating point errors well documented.

Next problem is caused by the fact you think that a string containing only digits can be used in mathematical expressions. This is not true and works (sometime) only if you set Option Strict Off in your program options.
This settings was left to Off to facilitate the porting of VB6 programs to VB.NET and you should set it to ON for new code to avoid the subtle bugs introduced by automatic conversion of values.
You should always convert that string to a numeric variable, do the math with the numeric variable and then, if you need to display the result, convert back the number to a string.

Public Class Income_Tax 

    Dim rate As Decimal 
    Dim difference As Decimal

    Private Sub textboxqitni_TextChanged(sender As Object, e As EventArgs) Handles textboxqitni.TextChanged

        ' Use a decimal variable to extract the current value typed
        Dim value As Decimal

        ' You don't want to continue if the input is not a valid number
        if Not decimal.TryParse(textboxqitni.Text, value) Then
            MessageBox.Show("Not a valid number")
            return
        End If

        ' Use AndAlso to express logical AND
        If value >= 0 AndAlso value <= 10000 Then
            value = value * 0.05

        ' <= 10000 is already takes, the elseif should be for > 10000
        ElseIf value > 10000 AndAlso value <= 30000 Then
            difference = value - 10000
            rate = difference * 0.1
            value = rate + 500
        ElseIf value > 30000 AndAlso value <= 70000 Then
            difference = value - 30000
            rate = difference * 0.15
            value = rate + 2500
        ElseIf value > 70000 AndAlso value <= 140000 Then
            difference = value - 70000
            rate = difference * 0.2
            value = rate + 8500
        ' ..............................
        ' Complete with other else if
        ' ..............................
        End If

        ' Finally set the value back to textbox
        textboxittd.Text = value.ToString()
    End Sub
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I did it! Thank you so much. I initially thought that I should use double since I'm presenting money thinking it would be the same as decimal and more precise. And what is option strict off? Anyway after inputting all the values, I clicked the clear button and it cleared all fields but gave me a message that the number is not valid. Everytime I click on it, it shows up. – Withershins Dec 29 '17 at 12:26
  • Simply comment the MessageBox.Show line – Steve Dec 29 '17 at 12:41
0

Try this

Try
Dim tbval As Integer = Integer.Parse(textboxqitni.Text)
If tbval >= 0 And tbval <= 10000 Then
    textboxittd.Text = tbval * 0.05
ElseIf tbval >= 10000 And tbval <= 30000 Then
    difference = tbval - 10000
    rate = difference * 0.1
    textboxittd.Text = rate + 500
ElseIf tbval >= 30000 And tbval <= 70000 Then
    difference = tbval - 30000
    rate = difference * 0.15
    textboxittd.Text = rate + 2500
ElseIf tbval >= 70000 And tbval <= 140000 Then
    difference = tbval - 70000
    rate = difference * 0.2
    textboxittd.Text = rate + 8500
ElseIf tbval >= 140000 And tbval <= 250000 Then
    difference = tbval - 140000
    rate = difference * 0.25
    textboxittd.Text = rate + 22500
ElseIf tbval >= 250000 And tbval <= 500000 Then
    difference = tbval - 250000
    rate = difference * 0.3
    textboxittd.Text = rate + 50000
ElseIf tbval >= 500000 And tbval <= 999999999999999 Then
    difference = tbval - 500000
    rate = difference * 0.32
    textboxittd.Text = rate + 125000
End If
Catch ex As Exception
    MsgBox("Error Occured" & vbCrLf & ex.Message)
End Try
maddy23285
  • 738
  • 6
  • 16
  • I did this and it gave me an error saying FormatException was unhandled. Whenever I click the clear button to clear all text fields and combobox this error pops up. – Withershins Dec 29 '17 at 11:36
  • Private Sub buttonbtmClear_Click(sender As Object, e As EventArgs) Handles buttonbtmClear.Click textboxitnoc.Clear() comboboxitq.SelectedIndex = -1 textboxityear.Clear() textboxqitni.Clear() textboxittd.Clear() End Sub – Withershins Dec 29 '17 at 11:59
  • comboboxitq.SelectedIndex can't be -1 it should be 0 – maddy23285 Dec 29 '17 at 12:02
  • When I removed textboxqitni.clear() in my code and ran it, it worked perfectly. No error. It seems this line of code is not working together with the code I wrote in my post. How the input be cleared from that textbox when someone clicks clear without giving me this error? – Withershins Dec 29 '17 at 12:12
  • ok try this one: textboxqitni.Text=0 or textboxqitni.Text="" instead of textboxqitni.Clear() – maddy23285 Dec 29 '17 at 12:15