Visual studio 2015 从字符串转换为整数会引发异常。Visual Studio';15

Visual studio 2015 从字符串转换为整数会引发异常。Visual Studio';15,visual-studio-2015,Visual Studio 2015,所以这里的问题是,当我按下开始运行计算的“OK”按钮时,它会为intHour=Convert.ToDecimal(txtbHoursReq.Text)引发和异常,我试图将其更改为、Toint16和Toint32,但它仍然引发异常。我不知道我做错了什么。我已经在谷歌上搜索过了,看了一遍我的书,看了一些例子,但还是弄不明白。任何帮助都将不胜感激。多谢各位 Sub calculateCost() ' This function calculates the cost to rent a y

所以这里的问题是,当我按下开始运行计算的“OK”按钮时,它会为intHour=Convert.ToDecimal(txtbHoursReq.Text)引发和异常,我试图将其更改为、Toint16和Toint32,但它仍然引发异常。我不知道我做错了什么。我已经在谷歌上搜索过了,看了一遍我的书,看了一些例子,但还是弄不明白。任何帮助都将不胜感激。多谢各位

Sub calculateCost()
    '   This function calculates the cost to rent a yacht depending on size and hours
    Dim sessionPrice As Integer
    Dim intHour As Integer
    Dim intYachtLength As Integer

    '   This functiin determines the price depending on length
    intYachtLength = lboxLength.SelectedIndex()
    Select Case intYachtLength
        Case 0
            intYachtLength = 95.0
        Case 1
            intYachtLength = 137.0
        Case 2
            intYachtLength = 160.0
        Case 3
            intYachtLength = 192.0
        Case 4
            intYachtLength = 250.0
        Case 5
            intYachtLength = 400.0
        Case 6
            intYachtLength = 550.0
    End Select

    '   Converts txtbHoursReq into an integer, then calculates the price of rental based on size of yacht and time of rental
    lblCostLength.Text = intYachtLength
    intHour = Convert.ToDecimal(txtbHoursReq.Text)' Throws an exception
    sessionPrice = intYachtLength * intHour
    lblDisplayPrice.Text = sessionPrice



End Sub

您可以设置一个断点并查看txtbHoursReq.Text中实际传递的内容吗?我做了一个原型,不明白如果文本框中的值是一个数字的表示,为什么它会抛出一个异常:这里是唯一可能的异常,您得到了哪一个

public void ConvertStringDecimal(string stringVal)
    {
        decimal decimalVal = 0;

        try
        {
            decimalVal = System.Convert.ToDecimal(stringVal);
            Response.Write(decimalVal);
        }
        catch (System.OverflowException)
        {
            Response.Write("System.OverflowException.");
        }
        catch (System.FormatException)
        {
            Response.Write("System.FormatException");
        }
        catch (System.ArgumentNullException)
        {
            Response.Write("System.ArgumentNullException");
        }
    }

我收到一个未处理的格式化异常System.FormatException“或出现在mscorlib.dll中,这意味着通过该文本框输入的任何内容都不是十进制的字符串表示形式。该字符串必须仅由数字和/或小数点组成。正确,我忘了提及,我正在测试以确保在文本框中没有输入数据时我的MsgBox出现。因此,当我按下“确定”按钮,论坛中没有数据,它抛出此异常。当有数据时,尽管它工作正常。很抱歉,我忘了说这句话。所以我没有弄清楚确切的问题,但看看我以前编写的代码,我只是将其换成intHour=Val(strNumHours)谢天谢地,它成功了,现在不会抛出异常。谢谢你的帮助。