简体   繁体   中英

In VB.NET why should I use Select, instead of If?

I've recently graduated and started a real job. In our training they've been exposing us to VB.NET and a lot of the features they use here. In some of the examples, they've used Select statements (and in a few places they were used where an If/Else really should have been used).

The only time that I've used a switch/select statement in other languages (other than assignments that required it) has been when I wanted the fall through to the next statement.

Given than VB.NET has no fall through, what (if any) cases are there to use the Select statement? Are there any cases when it provides advantages over and If/ElseIf statement?

Select Case , not just Select .
To me, it's one of the best features of the language.

  1. It's much more visual when you have several possible values to test against.

     select case some_var case 1 something() case 2 something_else() case 3 etc() end select
  2. It's much more readable when it comes to testing ranges:

     select case some_var case 1 to 10 something() case 20 to 30 something_else() case is > 100 etc() end select
  3. It's much more readable when you have a bunch of more complex conditions to test, making sure only one is selected:

     select case true case string.isnullorempty(a_string) something() case a_string.length < 5 something_else() case a_string = b_string etc() end select
  4. It's superior to C/C++ switch in the sense that it allows expressions as branching points, not just constants.

  5. When using constants as branching points (example 1), compiler is able to generate a more optimised code with direct jumps.

Select tells the compiler that every compare (If) in the analogous set of If/Else blocks is on the same value, and this allows it to make certain optimizations that are harder to be sure of otherwise. For example, it might be more eager to generate machine code that holds that value in a cpu register (that's just hypothetical... different compilers can do what they want).

Also, some of us find Select a lot more readable. It is important to follow the coding standards of whatever team or unit you find yourself.

First off, VB does have fall through, it's just not as obvious. The "fallthrough" in VB is just setting one case to have multiple values:

Dim number As Integer = 8
Select Case number
    Case 6,7,8
        ' do stuff
    Case Else
        ' do default stuff
End Select

As for its advantages, it's way easier to write one Select statement than say, more than three If/ElseIf statements that all test against the same value.

If you are going to do several different things based on the input comparison/range comparison if it's if-elseif+ then use Select instead of crazy if-elseif blocks.

VB.NET's Select statement has some cool features as well, so ensure that you know all the features.

There is a situation where Select Case can be much more efficient than If: when you have a list of "Or" clauses in the If condition and you do not need to evaluate them all to establish the truth of the condition. Suppose you had an if statement such as this:

If A() Or B() Then
    DoSomething()
ElseIF C() or D() Then
    DoSomethingElse()
Else
    DoTheDefault()
EndIF

In this case, to evaluate the first if statement, both functions A() and B() are executed, and similarly for the second if statement when A() and B() are both false. If A() returns true, then the value of B() is immaterial and, unless it changes the program state in a way that you actually want it to (generally not good practice), the execution of B() is redundant. The compiler is constrained by the requirement that all parts of the test MUST be executed before concluding on a value (optimisation of the test is not allowed according to the language spec).

You could separate the conditions into multiple IfElse statements to optimise it yourself but this makes the code less readable and increases the danger of errors when changes are made later. I find that using Select Case is better in this situation:

Select Case True
    Case A(), B()
        DoSomething()
    Case C(), D()
        DoSomethingElse()
    Case Else
        DoTheDefault()
End Select

Now, if A() returns True then B() is not evaluated at all. The evaluation of the conditions is in the sequence listed, so you can help to optimise your code by putting the tests in the order of most likely to return True or least expensive to execute (depending on the application).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM