Total .NET Analyzer Rule Documentation |
|
Avoid using Stop statements in code.
Remarks
Visual Basic .NET allows you to use Stop statements as a programmatic alternative to using a breakpoint in code. Like breakpoints, Stop statements halt code execution and cause a debugger exception. Unlike breakpoints, however, Stop statements are a permanent part of the code base and halt execution regardless of whether the code was compiled in debug or release mode.
Because of this, using Stop statements may cause serious problems in your code, including:
1. Any code that you have in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. This means that your objects may not be correctly closed or terminated.
2. Objects created at run time are destroyed and, files are closed, and object references are invalidated.
Resolution
You should avoid using Stop statements in code. If you need to implement a code break in Development mode, you should use a breakpoint. Using a breakpoint is a safer way to debug code, since a Stop statement will break into code regardless of whether it was compiled in debug or release mode.
If the intent is to exit code, you should replace the Stop statement with code that unloads all forms to ensure that objects are closed properly. Note that classes often control resources that are not released when the class is finalized. In order to release the most resources, you may wish to call the Dispose method, which finalizes the object and removes the associated COM+ reference. When you are finished using an object, call the Dispose method to release resources and perform tasks such as closing files and database connections. Unlike the Finalize destructor, the Dispose method must be called explicitly.
See Also
Stop Statement