Visual studio I';“我得到了”;已处理无效的强制转换异常";而不是我试图在屏幕上显示的消息框

Visual studio I';“我得到了”;已处理无效的强制转换异常";而不是我试图在屏幕上显示的消息框,visual-studio,Visual Studio,我的目标是在计算器中键入字母时,得到一条类似“输入数字”的消息,但问题是,它没有使用我编写的IsNumeric函数,而是显示“Microsoft.VisualBasic.dll中发生了“System.InvalidCastException”类型的未处理异常。” 我有一个严格的选择,它必须继续。“计算”按钮的代码如下所示。我试着做的嵌套Ifs就在那里,这样它就表明只有正数和有效的数字才能工作。是什么阻止消息框出现的。显示“System.InvalidCastException”的位置在intRa

我的目标是在计算器中键入字母时,得到一条类似“输入数字”的消息,但问题是,它没有使用我编写的IsNumeric函数,而是显示“Microsoft.VisualBasic.dll中发生了“System.InvalidCastException”类型的未处理异常。” 我有一个严格的选择,它必须继续。“计算”按钮的代码如下所示。我试着做的嵌套Ifs就在那里,这样它就表明只有正数和有效的数字才能工作。是什么阻止消息框出现的。显示“System.InvalidCastException”的位置在intRadius=CInt(txtInputRadiusValue.Text)旁边

Private子btnCalculateArea\u单击(发送者作为对象,e作为事件参数)处理btnCalculateArea。单击
Dim intRadius作为整数
内孔=0
将intVolume设置为整数
intVolume=0
常数dblPi为双精度=3.14159
作为字符串的Dim strNumber
strNumber=“String.Empty”
如果内径>=0,则
如果是数字(strNumber),则
'使文本框的内容与半径相等
intRadius=CInt(txtInputRadiusValue.Text)
其他的
MessageBox.Show(“您输入的号码无效。请输入一个有效的号码。”)
如果结束
'计算球体的体积
intVolume=CInt(1.333*dblPi*(内径^3))
'在标签中显示答案
lblRectangularBox.Text=CType(intVolume,字符串)
其他的
如果内径<0,则
MessageBox.Show(“此计算器不能使用负数。请输入一个非负数的数字。”)
其他的
如果结束
如果结束
端接头

我认为您的问题在于您的实例化:

strNumber = "String.Empty"

    If intRadius >= 0 Then
        If IsNumeric(strNumber) Then
            'To make the contents of the textbox what the radius equals
            intRadius = CInt(txtInputRadiusValue.Text)
        Else

            MessageBox.Show("The number you have enterd is invalid. Please enter a number that is valid.")

        End If
我可能错了,但是当你做If is numeric时,在我看来你还没有让用户输入他/她的值。为什么不实例化
strNumber=“0”

我想这可能会解决你的问题。MSDN文档说明,如果表达式是可以成功转换为数字的字符或字符串,则IsNumeric函数也会返回True。因为他们没有说任何关于空字符串值的内容,所以我会远离这些内容

希望有帮助

strNumber = "String.Empty"

    If intRadius >= 0 Then
        If IsNumeric(strNumber) Then
            'To make the contents of the textbox what the radius equals
            intRadius = CInt(txtInputRadiusValue.Text)
        Else

            MessageBox.Show("The number you have enterd is invalid. Please enter a number that is valid.")

        End If