Exit from Methods

Methods execution typically completes when the End Sub or End Function statements are encountered. You often need to break methods execution before the execution completes. In the case of Sub methods, you can accomplish this using the Exit Sub statement. The following example checks the value of an integer and immediately breaks if the value is greater than 10. If not, it loops until the value is 10 and then breaks:

Sub TestingValues(ByVal anInteger As Integer)    If anInteger > 10 Then     Exit Sub    ElseIf anInteger < 10 Then     Do Until anInteger = 10        anInteger += 1     Loop     Exit Sub    End IfEnd Sub

You can also use the Return keyword without a value instead of Exit Sub. For Function ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.