System.InvalidCastException问题

时间:2017-12-29 11:04:21

标签: vb.net

我无法理解如何修复代码。我一直收到这个错误。

  

InvalidCastException未处理   类型' System.InvalidCastException'的未处理异常发生了   在Microsoft.VisualBasic.dll中   附加信息:从字符串转换""输入' Double'是   无效。

我对如何解决这个问题感到困惑。我不明白这个错误。它始于If的开头。这是我正在使用的代码:

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

2 个答案:

答案 0 :(得分:2)

我建议的第一件事是使用十进制数据类型进行计算 您的数学运算似乎涉及货币值,在这种情况下,您应始终使用十进制数据类型来避免浮点错误well documented

下一个问题是由您认为只包含数字的字符串可用于数学表达式的事实引起的。这不是正确的,只有在程序选项中设置Option Strict Off时才有效(有时) 此设置保留为Off以便于将VB6程序移植到VB.NET,您应该将其设置为ON以获取新代码,以避免因自动转换值而引入的细微错误。
您应始终将该字符串转换为数字变量,使用数字变量进行数学运算,然后,如果需要显示结果,请将数字转换回字符串。

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

答案 1 :(得分:0)

试试这个

#!/usr/bin/env bash

sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" /home/vagrant/.bashrc
. /home/vagrant/.bashrc