VB Select Case if if textbox string,elif textbox numeric(isnumeric not working)

时间:2015-10-15 12:14:43

标签: vb.net select-case

使用必须以不同方式处理用户输入的程序,取决于数字还是字符串。 Select Case和IsNumeric未按预期工作。

当animal = char或string时,我得到这段代码。

错误:

  

未处理的类型' System.InvalidCastException'发生在Microsoft.VisualBasic.dll

     

附加信息:从字符串转换" 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

3 个答案:

答案 0 :(得分:4)

你应该查看你不放的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语句中使用字符串。此外,严格打开该选项;)

答案 1 :(得分: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

答案 2 :(得分: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。取决于应用程序和本地政策的重要性。