vb.netselect-case

How to use SELECT CASE with TO using INTEGER


I am trying to use SELECT CASE in my code.

but even the value of the variable is in between the set value in SELECT CASE it does not hit the code inside that case.

for example:

Dim i as Integer = 27

Select Case i
   Case 3 To 33
      'This would be Hit
   Case 34 To 50 
      'Some Code
   Case Else
End Select

Now. The code below in comment This would be hit cannot be executed.

How to fix it?

Regards,


Solution

  • Either you are mistaken or something on your system is broken. I just tested this code in a Console project:

    Module Module1
    
        Sub Main()
            Dim i As Integer = 27
    
            Select Case i
                Case 3 To 33
                    'This would be Hit
                    Console.WriteLine("i is in the range 3 to 33")
                Case 34 To 50
                    'Some Code
                    Console.WriteLine("i is in the range 34 to 50")
                Case Else
                    Console.WriteLine("i is in not in either range")
            End Select
    
            Console.ReadLine()
        End Sub
    
    End Module
    

    I saw the message:

    i is in the range 3 to 33

    as expected.