【问题标题】:VB Select Case if textbox string, elif textbox numeric (isnumeric not working)VB Select Case if textbox string, elif textbox numeric (isnumeric not working)
【发布时间】:2015-10-15 12:14:42
【问题描述】:

我使用的程序必须区别对待用户输入,取决于数字还是字符串。 Select Case 和 IsNumeric 未按预期工作。

当 animal=a char 或 string 时,我得到此代码。

错误:

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

【问题讨论】:

  • 我能知道你想让代码做什么吗?

标签: vb.net select-case


【解决方案1】:

您应该查看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 语句中使用字符串。另外,请严格打开该选项;)

【讨论】:

  • 我们需要一个国际期权严格意识日。还有一条丝带,绝对是一条丝带。
  • @Plutonix 这条评论让我很开心!
【解决方案2】:

您可以使用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

【讨论】:

  • 第一个说法不正确。 IsNumeric() 计算字符串,如果是数字则返回 true。它还执行 Trim() 并忽略一些空格,例如 vbCrlf。
  • @rheitzman 是的,我后来意识到了。但在这种情况下,它似乎不起作用。固定,
【解决方案3】:

另一种编码方法...

    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。取决于应用的重要性和本地政策。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2018-09-11
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多