选择数字之间的Case

时间:2017-05-30 13:45:03

标签: vb.net select-case

我尝试在数字之间选择一个案例。每个150000代码Textbox5在点击按钮上更改。

Select Case TextBox5.Text
    Case 0 To 150000
        TextBox6.Text = "-"
    Case 150001 To 300001
        TextBox6.Text = "+1-"
    Case 300002 To 450002
        TextBox6.Text = "+2-"
    Case 450003 To 600003
        TextBox6.Text = "+3-"
    Case 600004 To 750004
        TextBox6.Text = "+4-"
    Case 750005 To 900005
        TextBox6.Text = "+5-"
    Case 900006 To 1050006
        TextBox6.Text = "+6-"
    Case Else
        TextBox6.Text = "+Extra-"
End Select

当我尝试900006到1050006之间的任何数字时,我有“+额外”而不是“+ 6-” 并尝试超过1050006我有“ - ”

4 个答案:

答案 0 :(得分:8)

这很有趣。但在我尝试找出导致的问题之前,我会为您提供正确的方法:String不是数字,将Option Strict设置为On并学会使用类型安全码。不要让编译器猜出你想要实现的目标。

您可以使用Int32.TryParse

Dim number As Int32
If Not Int32.TryParse(TextBox5.Text, number) Then
    MessageBox.Show("Please enter a valid integer")
    Return
End If

Select Case number ' now integer is the target type
    Case 0 To 150000
        TextBox6.Text = "-"
    Case 150001 To 300001
        TextBox6.Text = "+1-"
    Case 300002 To 450002
        TextBox6.Text = "+2-"
    Case 450003 To 600003
        TextBox6.Text = "+3-"
    Case 600004 To 750004
        TextBox6.Text = "+4-"
    Case 750005 To 900005
        TextBox6.Text = "+5-"
    Case 900006 To 1050006
        TextBox6.Text = "+6-"
    Case Else
        TextBox6.Text = "+Extra-"
End Select

这将使用Option Strict On进行编译和编译。

现在为什么你的代码不起作用。如果您将最后一个范围更改为Case 900006 To 999999,它将按预期工作。这必须做如何比较字符串。即使这会编译(严格关闭),Case也会将范围900006 To 1050006视为字符串,而不是"900006" To "1050006",因此将它们从左到右进行字母比较,sinvce {{1} }比"9"“更大”这个条件永远不会成立,这就是为什么你永远不会进入最后"1"但进入Case

Documentation

  

表达式列表中的表达式可以是任何数据类型,提供   它们可以隐式转换为testexpression的类型和   适当的比较运算符对于它所处的两种类型是有效的   与。一起使用。

使用Case Else Option Strict Off(范围)转换为expressionlist的类型testexpression(因为String)。 使用TextBox5.Text您正确收到编译错误,因为Option Strict On不是String

  

Option Strict On禁止从“整数”到的隐式转换   '字符串'

答案 1 :(得分:0)

我同意Tim Schmelter的观点。补充答案你应该使用

Select Case CLng(TextBox5.Text)

有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions,当您处于此状态时,请避免使用TextBox5

之类的ID

答案 2 :(得分:0)

这里是执行此操作的另一种方法,并且如果使用不支持“ To”运算符的ASP Classic,它也可以使用。此代码还显示了在第一个Select / Case中嵌套的Select / Case结构的使用:

CardName = Left(CardNumber, 4)
Select Case CardName
    Case 1800
        GetCardName = "JCB (Japanese Credit Bureau)"
    Case 2014
        GetCardName = "enRoute"
    Case 2131
        GetCardName = "JCB (Japanese Credit Bureau)"
    Case 2149
        GetCardName = "enRoute"

    'Case 3000 To 3059:
    Case (CardName => 3000 And CardName <= 3059)
        GetCardName = "Diners Club"
    'Case 3400 To 3499
    Case (CardName => 3400 And CardName <= 3499)
        GetCardName = "American Express"
    'Case 3528 To 3589
    Case (CardName => 3528 And CardName <= 3589)
        GetCardName = "JCB (Japanese Credit Bureau)"
    'Case 3600 To 3699
    Case (CardName => 3600 And CardName <= 3699)
        GetCardName = "Diners Club"
    'Case 3700 To 3799
    Case (CardName => 3700 And CardName <= 3799)
        GetCardName = "American Express"
    'Case 3800 To 3889
    Case (CardName => 3800 And CardName <= 3889)
        GetCardName = "Diners Club"
    'Case 3890 To 3899
    Case (CardName => 3890 And CardName <= 3899)
        GetCardName = "carteBlanche"
    'Case 4000 To 4999
    Case (CardName => 4000 And CardName <= 4999)
        GetCardName = "VISA"
    'Case 5100 To 5599
    Case (CardName => 5100 And CardName <= 5599)
        GetCardName = "MasterCard"

    Case 5610
        GetCardName = "Australian BankCard"
    Case 6011
        GetCardName = "Discover"

    Case Else

            Select Case Left(CardNumber, 1)
                Case 1 'Airline
                    GetCardName = "Other (Airline)"
                Case 2 'Airline and other industry assignments
                    GetCardName = "Other (Airline and other idustries)"
                Case 3 'Travel/Entertainment
                    GetCardName = "Other (Travel/Entertainment)"
                Case 4 'Banking and financial
                    GetCardName = "Other (Banking/Financial)"
                Case 5 'Banking and financial
                    GetCardName = "Other (Banking/Financial)"
                Case 6 'Merchandising and banking
                    GetCardName = "Other (Merchandising/Banking)"
                Case 7 'Petroleum
                    GetCardName = "Other (Gas Company)"
                Case 8 'HealthCare/Telecommunications and other
                    GetCardName = "Other (HealthCare/Telecomm/Etc)"
                Case 9 'National assignment
                    GetCardName = "Other [Card Carrier Unknown]"
                Case 0 'ISO/TC 68 and other industry assignments
                    GetCardName = "Other [Card Carrier Unknown]"
        End Select
End Select

答案 3 :(得分:-1)

Dim i as Long
If Long.TryParse(TextBox5.Text, i)
  Select Case i
    Case 0 To 150000
      TextBox6.Text = "-"
    Case 150001 To 300001
      TextBox6.Text = "+1-"
   Case 300002 To 450002
      TextBox6.Text = "+2-"
    Case 450003 To 600003
      TextBox6.Text = "+3-"
    Case 600004 To 750004
      TextBox6.Text = "+4-"
    Case 750005 To 900005
      TextBox6.Text = "+5-"
    Case 900006 To 1050006
      TextBox6.Text = "+6-"
    Case Else
      TextBox6.Text = "+Extra-"
  End Select
Else
  TextBox6.Text = "Not a Number"
End If

根据您的要求进行修改。