VB.NET 中的多个选择案例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13970603/
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
Multiple select cases in VB.NET
提问by user1295053
I have tried the below:
我尝试了以下方法:
Select Case Combo1.SelectedItem Or Combo2.SelectedItem
But I get the error:
但我收到错误:
Conversion from String "string here" to type 'Long' is not valid
Is it possible to have multiple select cases?
是否可以有多个选择案例?
回答by Guffa
You separate multiple values by using a comma:
您可以使用逗号分隔多个值:
Case Combo1.SelectedItem, Combo2.SelectedItem
Using Or
would make it an expression that would be evaluated before compared to the value in the Select
.
UsingOr
将使它成为一个表达式,该表达式将在与Select
.
If your value in the Select
is a Long
value, then you may need to convert the strings from the controls:
如果您的值Select
是一个Long
值,那么您可能需要从控件转换字符串:
Case CLng(Combo1.SelectedItem), CLng(Combo2.SelectedItem)
To address the question directly, using multiple values as a test expression in a select is not possible:
要直接解决这个问题,在选择中使用多个值作为测试表达式是不可能的:
Select Case v1, v2 'Not possible
回答by Ralph Maurmeier
Hi Googled and saw this question without an answer. Upon further research, I found this to work for my purposes.
嗨,谷歌搜索,看到这个问题没有答案。经过进一步研究,我发现这对我的目的有用。
Basically, you start with:
基本上,你开始:
Select case True
选择大小写 True
Then each case statement can be combinations of the two variables. When both are met, the case is true and will execute the appropriate code.
那么每个case语句都可以是两个变量的组合。当两者都满足时,情况为真,将执行适当的代码。
https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters
https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters
回答by Suren Babayan
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim value As Integer
For i = 1 To 3
For j = 1 To 5
value = (GetCode(i, j))
TextBox1.Text = TextBox1.Text & "i=" & i & "->j=" & j & "=" & value & vbCrLf
Next
Next
End Sub
Function GetCode(ByVal v1 As Integer, ByVal v2 As Integer) As Integer
Dim retval As Integer
Dim forselect As String
forselect = v1 & v2
Select Case forselect
Case 11
retval = 11
Case 12
retval = 12
Case 13
retval = 13
Case 14
retval = 14
Case 15
retval = 15
Case 21
retval = 21
Case 22
retval = 22
Case 23
retval = 23
Case 24
retval = 24
Case 25
retval = 25
Case 31
retval = 31
Case 32
retval = 32
Case 3, 3
retval = 33
Case 34
retval = 34
Case 35
retval = 35
End Select
Return retval
End Function