0

我正在使用必须以不同方式处理用户输入的程序,这取决于数字还是字符串。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
4

3 回答 3

3

您应该查看未放置的Select Case的文档或者,您放置了一个逗号。

Case "D", "d"

或者将比较的字符串小写。

Select Case stranimal.ToLower()
    Case "d"
        msgLabel.Text = "Dog"
    Case "c"
        msgLabel.Text = "Cat"
    Case Else
End Select

decanimal 是小数,不要在 case 语句中使用字符串。另外,请严格打开该选项;)

于 2015-10-15T12:19:28.600 回答
1

您可以使用的返回值Double.TryParse()来查看数据是否为数字。这是更正后的代码:-

Public Sub ifButton_Click(sender As Object, e As EventArgs) Handles ifButton.Click
    Dim animal As String = codeTextBox.Text
    Select Case Double.TryParse(animal, Nothing) 'See if it is convertible to Double (numeric) or not.
        Case True
            Select Case Double.Parse(animal)

                Case 1
                    msgLabel.Text = "Dog"
                Case 2
                    msgLabel.Text = "Cat"
                Case Else
                    msgLabel.Text = "Bird"
            End Select
        Case False
            Select Case animal.ToLower() 'To compare the strings without case-sensitivity
                Case "d"
                    msgLabel.Text = "Dog"
                Case "c"
                    msgLabel.Text = "Cat"
                Case Else
                    'You didn't mention anything but I guess it should be msgLabel.Text = "Bird"
            End Select
    End Select
End Sub
于 2015-10-15T13:57:37.487 回答
0

另一种编码方法...

    Dim cAnimals As New Collection
    cAnimals.Add("Dog", "d")
    cAnimals.Add("Dog", "1")
    cAnimals.Add("Cat", "c")
    cAnimals.Add("Cat", "2")

    Dim s As String = ""
    Do While True
        s = InputBox("code:").ToLower
        If s = "" Then Exit Do
        If cAnimals.Contains(s) Then
            MsgBox(cAnimals(s))
        Else
            MsgBox("Invalid code")
        End If
    Loop

使用一些数据结构来存储转换代码,这里是一个VB Collection,然后检查代码是否在数据中。

VB 并不真正关心数据是否为数字。这有时会咬人,但在其他时候很有用。Option Strict 将失败,在大多数情况下并不真正需要 IMO。取决于应用程序的重要性和本地政策。

于 2015-10-15T20:59:48.790 回答