user2721815 user2721815 - 5 months ago 12
Vb.net Question

In VB.NET is "Exit Sub" necessary or helpful?

When using Sub's, if I'm not returning a value, is it necessary to use:

Public Sub whatever()
...
Exit Sub
End Sub


instead of just

Public Sub whatever()
...
End Sub


Do I gain anything (memory, speed, etc.) by using "Exit"?

Does the Sub exit anyway when it is done even if I don't use the "Exit" statement?

Thanks.

Answer

In this particular case Exit Sub is completely unnecessary. It can be used there but is generally considered bad style. The statement is necessary when you want to prematurely leave the method. For example if you detect a specific condition is met and you don't want to execute the rest of the method

Public Sub Example() 
  If SomeCondition Then 
    Exit Sub
  End If

  ' Do other work
End Sub
Comments