Several cases of choice in VB.NET

I tried the following:

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 several sample cases?

+6
source share
3 answers

You separate multiple values ​​with a comma:

 Case Combo1.SelectedItem, Combo2.SelectedItem 

Using Or will make it an expression that will be evaluated before comparing with the value in Select .

If your Select value is a Long value, you may need to convert the strings from the controls:

 Case CLng(Combo1.SelectedItem), CLng(Combo2.SelectedItem) 

To directly ask a question, using multiple values ​​as a test expression in a selection is not possible:

 Select Case v1, v2 'Not possible 
+21
source

Hi, Googled and saw this question unanswered. After further research, I found this to work for my purposes.

Basically, you start with:

Choose True

Then each operator can be a combination of two variables. When both are executed, the case is correct and will execute the corresponding code.

https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters

0
source
 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 
-2
source

All Articles