views:

442

answers:

3

Here are a couple of questions i gathered regarding exit select...

  1. Is there any reason for using exit select in VB.NET?
  2. The reason has anything to do with performance?
  3. The exit select is equal to break;?

Example 1

Select case Name
case "Mary"
'...
case "John"
'...
case else

end select

Example 2

Select case Name
case "Mary"
'...
exit select

case "John"
'...
exit select

case else

end select
A: 

The reason to exit is to not evaluate any other case statements, and yes this can be tied to performance although probably not noticeable in most cases. And for your third question, yes it is identical to a break; in other languages.

jaywon
Yea, unless you have a huge list of cases in your select, or you hit it with huge amounts of data, you will never notice the performance difference.
BBlake
No, it's not the same, because the statements don't fall through if you omit the break.
Joel Coehoorn
What I really cannot understand the performance issue. I have never written a select statement with more than 10 cases
Chocol8
There is no real performance issue.
Joel Coehoorn
@Joel: I read hat a break is already implied. In other words, if no conditions are used (like in example 2), exit select is useless in VB.NET?
Chocol8
As Joel stated, this answer is simply wrong.
Meta-Knight
@strakastroukas: That's right.
Meta-Knight
@Joel- What you meant to say is that statements dont fall through if you omit the 'exit select'
TStamper
@Meta-Knight- this answer is not wrong, it is just misleading, because the 'exit select' in vb does act as a 'break' in c#
TStamper
@TStamper: "The reason to exit is to not evaluate any other case statement" -> Wrong... "Yes this can be tied to performance" -> That's not what it's used for most of the time... "yes it is identical to a break; in other languages." maybe in functionality but not in the way you use it...
Meta-Knight
Overall I wouldn't call this a correct answer ;-)
Meta-Knight
@Meta- very true- I wouldn't call it a correct answer either, exactly why I didnt upvote it, but what I was referring to was the last statement, the first two statements are different story
TStamper
@all - thanks for the comments guys, i answered it as i understood it, if that is wrong i am happy to hear the right answers. If in fact an exit select is implied after a match, then that is tied to my second comment. I thought that without an exit select, all statements are evaluated, which in turn is tied to performance, although like i said, nothing noticeable.
jaywon
+2  A: 

Well.. is like using a goto.. once you found the correct case there is no use in "exiting" the case since in vb will be go out. In C# you need to exit the case (in that case, with a break).

The point is that you can use it in the middle of the scope of a case, something like

Case 1  
   do something  
   do something  
   evaluate  
      exit select  
   else  
      do something

It's ugly, but you can do that...

gbianchi
+6  A: 

It's not the same as using the break keyword with switch statements from C-like languages. With a switch, if you omit the break control will fall through to the next case. With a vb Select, control does not fall through; a break is already implied.

However, you can use it as a guard clause, to avoid needing to nest code another level in an if block. For example:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If Not SomeCondition Then Exit Select
         ''# Do something
    Case SomeEnum.SomeValue2
         ''# Do something else
    Case Else
         ''# Default case
End Select

That's a little nicer than this equivalent code:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If SomeCondition Then
             ''# Do something
         End If
    Case SomeEnum.SomeValue2
         ''# Do something else
    Case Else
         ''# Default case
End Select

Any performance difference between these two samples is almost certainly insignificant compared to other factors.

One other use is if you have a lot of cases, and one of the cases is placed so that a match means you want to stop checking all the others. This already happens, and so you might just have an empty case statement there. But you might also add an Exit Select to make it clear to maintainers that you expect this case not to do anything else.

Joel Coehoorn
downvoter: any reason?
Joel Coehoorn
+1 for actually having a correct answer ;-)
Meta-Knight
I really thing that the second example is much nicer that the first one because the second one is Structured and the first one isn't. Instructions that break the execution flow (like Goto, Exits, etc) unstructure the code.
SoMoS
@SoMoS: I like to have guard clauses at the beginning of methods to check preconditions, but in the middle of a Select Case, I tend to agree with you that the second example is better in most cases.
Meta-Knight