Select 数字之间的大小写

Select Case between number

我尝试 select 数字之间的情况。每 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 之间的任何数字时,我得到的是“+Extra-”而不是“+6-” 并尝试超过 1050006 我有“-”

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

Select Case CLng(TextBox5.Text)

有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions,同时避免使用像 TextBox5 这样的 ID

这很有趣。但在我试图找出它的原因之前,我为您提供了正确的方法: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",因此从左到右逐个字母地比较它们,因为 "9" 是 "greater" 而不是 "1" 这种情况永远不会成立,这就是为什么你永远不会进入最后一个 Case 而是进入 Case Else.

Documentation:

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparison operator is valid for the two types it is being used with.

使用 Option Strict Offexpressionlist(范围)被转换为 testexpression 的类型,即 String(因为 TextBox5.Text)。 使用 Option Strict On 你会正确地得到一个编译器错误,因为 String 不是 Integer:

Option Strict On disallows implicit conversions from 'Integer' to 'String'

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

根据您的要求进行修改。

这是另一种方法,如果使用不支持 "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