Exit Sub Statement in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes how to use Exit Sub in Visual Basic .NET

The Exit Sub statement is an unconditional jump command that stops performing tasks early (if necessary).

It makes the program stop executing the current subroutine but unlike GoTo and such, cannot make the program jump around arbitrarily, possibly leading to spaghetti code.

Ex example of Exit Sub statement:

Private Sub MyTask(ByVal size As Integer)
    For i As Integer = 0 To size
        If i = 10 Then
            Exit Sub ' should stop the task early
        Else
            MyTextBox.Text &= "The value of i is: " & i.ToString & Environment.NewLine
        End If
    Next
End Sub                  

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.