vb.net 选择数字之间的大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44264037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Case between number
提问by Jamyz
I try a select case between number. Each 150000 the code the Textbox5 change on click button.
我尝试在数字之间选择一个案例。每 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
When I try any number between 900006 to 1050006 I have "+Extra-" not "+6-" and try over 1050006 I have "-"
当我尝试 900006 到 1050006 之间的任何数字时,我有“+Extra-”而不是“+6-”并尝试超过 1050006 我有“-”
采纳答案by Mike
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
Modify to your requirements.
根据您的要求进行修改。
回答by Tim Schmelter
This is interesting. But before I try to find out what it's causing, I provide you the correct way to do this: a Stringis not a number, set Option Strictto Onand learn to use type safe code. Don't let the compiler guess what you're trying to achieve.
这很有趣。但是在我尝试找出它的原因之前,我为您提供了正确的方法来做到这一点:aString不是数字,设置Option Strict为On并学习使用类型安全代码。不要让编译器猜测您要实现的目标。
You can use Int32.TryParse:
您可以使用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
This will work and compile also with Option Strict On.
这也可以使用Option Strict On.
Now why your code doesn't work. If you change the last range to Case 900006 To 999999it will work as expected. This has to do how strings are compared. Even if this compiles (Strict Off) the Casewill treat the range 900006 To 1050006as strings so as "900006" To "1050006", so they are compared letter for letter from left to right, sinvce "9"is "greater" than "1"this condition is never true, that's why you never get into the last Casebut into the Case Else.
现在为什么您的代码不起作用。如果您将最后一个范围更改为Case 900006 To 999999它会按预期工作。这与字符串的比较方式有关。即使编译 (Strict Off)Case也会将范围900006 To 1050006视为字符串"900006" To "1050006",因此它们会从左到右逐个字母进行比较,因为这个条件永远不会"9"“大于” "1",这就是为什么你永远不会进入最后一个Case但进入Case Else.
文件:
The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpressionand the appropriate comparison operator is valid for the two types it is being used with.
expressionlist 中的表达式可以是任何数据类型,前提是它们可以隐式转换为 testexpression 的类型,并且适当的比较运算符对正在使用的两种类型有效。
With Option Strict Offthe expressionlist(the range) is converted to the type of testexpressionwhich is String(because of TextBox5.Text).
With Option Strict Onyou correctly get a compiler error because a Stringis not an Integer:
用Option Strict Off的expressionlist(的范围)被转化为所述类型的testexpression,其是String(因为TextBox5.Text)。随着Option Strict On你正确地得到一个编译器错误,因为String不是Integer:
Option Strict On disallows implicit conversions from 'Integer' to 'String'
Option Strict On 不允许从“整数”到“字符串”的隐式转换
回答by Noel Lopes
I agree with Tim Schmelter. Complementing its answer you should use
我同意蒂姆·施梅尔特的观点。补充你应该使用的答案
Select Case CLng(TextBox5.Text)
See https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functionsfor details and while you are at it avoid ids like TextBox5
有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions并避免使用 TextBox5 之类的 id
回答by Bjorgen Eatinger
Here is another way to do this, and it also works if using ASP Classic, which does not support the "To" operator. This code also shows the use of a nested Select/Case structure, within the first Select/Case:
这是执行此操作的另一种方法,如果使用不支持“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

