tags:

views:

604

answers:

5
+1  Q: 

Select Case True

Apparently this used to be a way in VB6 and VBA to short circuit and execute the first true case:

Select Case True
End Select

Is this still in use (VB.NET) ?

+3  A: 

Do you mean something like this?

 Select Case True
    Case 1 = 0
        Console.Write("1")
    Case 1 = 1
        Console.Write("2")
    Case 2 = 2
        Console.Write("3")
End Select

In which, the program would write 2...if that's what you're asking, then yes, that is still around in VB.NET

Gus
+6  A: 

Question's a little brief, but the form he's asking about is often used instead of an If/ElseIf type block, some people find it a little easier to read. For example:

Select Case True
    Case testVariable < 0
         Console.Write("You must supply a positive value.")
    Case testVariable > 10
         Console.Write("Please enter a number from 0-10.")
    Case True
         Call DoWork(testVariable)
End Select

The answer is that yes, this still works in VB.NET. Just take care with when you use it, because it's not a "standard programming construct" and may be unfamiliar to people that have to maintain your code in the future.

Chad Birch
Yes, that's the one. Thanks!
Otávio Décio
+5  A: 

I'm not sure how this construct offers any advantages over the following:

If testVariable < 0 Then
     Console.Write("You must supply a positive value.")
ElseIf testVariable > 10 Then
     Console.Write("Please enter a number less than 10.")
Else
     Call DoWork(testVariable)
End If

The above structure is short-circuiting, and I don't have to try to work out what it does as it's a standard construct.

Patrick McDonald
+3  A: 

Others have already answered that actual question, but I just want to chime in that I use this construct fairly frequently. I think it's often the most readable way of simultaneously testing two boolean conditions:

Dim A As Boolean
Dim B As Boolean
'
'do stuff to set values of A and B
'
Select Case True
  Case A And B
    'something
  Case A And Not B
    'something else
  Case Not A And B
    'you get the picture
  Case Else
    '...
End Select

I admit that part of why I find it easily readable is that I do use it, and that I do recall having to parse it the first time I saw it--but once successfully parsed, my reaction was "That's brilliant!"

RolandTumble
+1  A: 

There is a lot of confusion on this topic, but to answer the OPs question: Yes, logical evaluation is the same in VB.Net as it is in VB6 as it is in VBA. http://support.microsoft.com/kb/817250

To take advantage of the Select Case optimization technique you use Select Cases inherent syntax to avoid the use of the Logical Operators And, Or, Xor, etc. It is these operators that do have Short Circuit evaluation.

Consider this example:

Public Sub Example()
    If A Or B Then
        Beep
    End If
    Select Case True
        Case A, B
            Beep
    End Select
End Sub

Private Function A() As Boolean
Debug.Print "A Ran"
    A = True
End Function

Private Function B() As Boolean
Debug.Print "B Ran"
    B = False
End Function

The Select Case version will only run A. The If-Block will run both. This is not If statement's fault, rather it is the fault of the And operator. If you prefer, you can structure the If Statement to short circuit like this:

Public Sub Example2()
    If A Then
    ElseIf B Then
        Beep
    End If
End Sub

And B will not run. It's all just a matter of style.

The important thing to know is that what you are avoiding is the And/Or/Xor Operators not the If-Blocks. If you like the Select Case version of the If-Block better... More power to you:)

Oorang