Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net VB选择文本框字符串的大小写,elif文本框数字(isnumeric不工作)_Vb.net_Select Case - Fatal编程技术网

Vb.net VB选择文本框字符串的大小写,elif文本框数字(isnumeric不工作)

Vb.net VB选择文本框字符串的大小写,elif文本框数字(isnumeric不工作),vb.net,select-case,Vb.net,Select Case,我使用的程序必须区别对待用户输入,这取决于数字还是字符串。Select Case和IsNumeric未按预期工作 当animal=char或string时,我得到这个代码 错误: Microsoft.VisualBasic.dll中发生类型为“System.InvalidCastException”的未处理异常 其他信息:从字符串“D”到类型“Long”的转换无效 故障代码: Case "D" Or "d" 所有代码: Option Explicit O

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

当animal=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

你应该看看文档,因为你没有放,或者,你放了一个逗号

Case "D", "d"
或者将比较的字符串放在小写字母中

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

decanimal是一个十进制数,不要在case语句中使用string。此外,请启用该选项;)

您应该查看文档,因为您没有放逗号

Case "D", "d"
或者将比较的字符串放在小写字母中

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

decanimal是一个十进制数,不要在case语句中使用string。此外,请启用该选项;)

可以使用返回值
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

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

另一种编码方法

    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集合,然后检查代码是否在数据中


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

另一种编码方法

    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集合,然后检查代码是否在数据中


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

我们需要一个国际选项严格意识日。还有一条丝带,绝对是一条丝带,这条评论让我很开心!我们需要一个国际意识日。还有一条丝带,绝对是一条丝带,这条评论让我很开心!我能知道你想要代码做什么吗?我能知道你想要代码做什么吗?第一条语句不正确。IsNumeric()计算字符串,如果它是数字,则返回true。它还执行Trim()并忽略一些空白,如vbCrlf。@rheitzman是的,我后来意识到了这一点。但在这种情况下,它似乎不起作用。已修复,第一个语句不正确。IsNumeric()计算字符串,如果它是数字,则返回true。它还执行Trim()并忽略一些空白,如vbCrlf。@rheitzman是的,我后来意识到了这一点。但在这种情况下,它似乎不起作用。固定的,