Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use exit select?

Tags:

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

  1. Is there any reason for using exit select in VB.NET?
  2. Does the reason have anything to do with performance?
  3. Is the exit select 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 
like image 767
OrElse Avatar asked Dec 17 '09 19:12

OrElse


People also ask

What is use of exit statement in Visual Basic?

The Exit statement transfers the control from a procedure or block immediately to the statement following the procedure call or the block definition. It terminates the loop, procedure, try block or the select block from where it is called.

How do I exit select case?

If the code within a Case or Case Else statement block does not need to run any more of the statements in the block, it can exit the block by using the Exit Select statement. This transfers control immediately to the statement following End Select .

What is select case used for?

A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.

How to Exit For loop in VB?

Exit For and Continue For The Exit For statement immediately exits the For … Next loop and transfers control to the statement that follows the Next statement. The Continue For statement transfers control immediately to the next iteration of the loop.


1 Answers

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 it will fall through to the next case. With a Visual Basic 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.

like image 121
Joel Coehoorn Avatar answered Sep 25 '22 11:09

Joel Coehoorn