我正在使用必须以不同方式处理用户输入的程序,这取决于数字还是字符串。Select Case 和 IsNumeric 未按预期工作。
当动物 = 字符或字符串时,我得到此代码。
错误:
Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的未处理异常
附加信息:从字符串“D”到类型“Long”的转换无效。
麻烦的代码:
Case "D" Or "d"
所有代码:
Option Explicit Off
Option Strict Off
Public Class MainForm
Public Sub ifButton_Click(sender As Object, e As EventArgs) Handles ifButton.Click
animal = codeTextBox.Text
Select Case IsNumeric(codeTextBox.Text)
Case True
Dim decanimal As Decimal
decanimal = CDec(animal)
Select Case decanimal
Case "1"
msgLabel.Text = "Dog"
Case "2"
msgLabel.Text = "Cat"
Case Else
msgLabel.Text = "Bird"
End Select
Case False
Dim stranimal As String
stranimal = CStr(animal)
Select Case stranimal
Case "D" Or "d"
msgLabel.Text = "Dog"
Case "C" Or "c"
msgLabel.Text = "Cat"
Case Else
End Select
End Select
End Sub
End Class