17

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1295053
  • 303
  • 1
  • 7
  • 17
  • .SelectedItem is not a boolean – Hrqls Dec 20 '12 at 10:57
  • By using `Or`, you will compare the both values on a binary level (adding bit by bit). So vb.net will try to convert the string to a type that can be compared on a binary level. The implicit convertion of vb.net will thus try to convert any type to `Long` for the binary comparition. But there is no implicit converter for `String` to `Long`. – Stef Geysels Jun 16 '23 at 05:59

3 Answers3

45

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.

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

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Dim test As String Select Case Combo1.SelectedItem,combo2.SelectedItem Case "1" test= "ND1" Case "2" test= "D1" Case "3" test= "ND2" Case Else Return Nothing End Select Return test – user1295053 Dec 20 '12 at 11:36
  • @user1295053: Aha, you are trying to use multiple values as the test expresion, not in a case. Sorry for the confusion. That is not possible. – Guffa Dec 20 '12 at 11:38
1

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

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.

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

Ralph Maurmeier
  • 101
  • 1
  • 11
-3
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
nhahtdh
  • 55,989
  • 15
  • 126
  • 162