Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

End Statement

Terminates execution immediately.



The End statement can be placed anywhere in a procedure to end code execution, close files opened with an Open statement, and clear variables. The End statement calls the Exit method of the Environment class in the System namespace. System.Environment.Exit requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs.

When executed, the End statement clears all variables at module and class level and all static local variables in all modules.

Note   The End statement stops code execution abruptly, without invoking the Finalize method or any other Visual Basic code. Object references held by other programs are invalidated.

The End statement provides a way to force your program to halt. Your program closes as soon as there are no other programs holding references to its objects and none of its code is executing.

If your application has any forms open, you should close them before executing End. A console application can either use End or simply return from the Main procedure.

With an additional keyword, End delineates the end of the definition of the appropriate procedure or block.


This example uses the End Statement to terminate code execution if the user requests it.

Sub Form_Load()
   ' ...
   Dim Answer As MsgBoxResult
   Answer = MsgBox("Do you want to quit now?", MsgBoxStyle.YesNo)
   If Answer = MsgBoxResult.Yes Then
      MsgBox("Terminating program")
   End If
   ' ...
End Sub

Smart Device Developer Notes

This statement is not supported.

See Also

End | Requesting Minimum Permissions | SecurityPermissionFlag Enumeration

© 2015 Microsoft