Visual Basic Concepts
Tips for Debugging
See Also
There are several ways to simplify debugging:
- When your application doesn't produce correct results, browse through the code and try to find statements that may have caused the problem. Set breakpoints at these statements and restart the application.
- When the program halts, test the values of important variables and properties. Use Quick Watch or set watch expressions to monitor these values. Use the Immediate window to examine variables and expressions.
- Use the Break on All Errors option to determine where an error occurred. To temporarily change this option, select Toggle from the Code window context menu, then toggle the option from the submenu. Step through your code, using watch expressions and the Locals window to monitor how values change as the code runs.
- If an error occurs in a loop, define a break expression to determine where the problem occurs. Use the Immediate window together with Set Next Statement to re-execute the loop after making corrections.
- If you determine that a variable or property is causing problems in your application, use a Debug.Assert statement to halt execution when the wrong value is assigned to the variable or property.
- To set the error trapping state that Visual Basic defaults to at the beginning of any debugging session, open the Options dialog box (available from the Tools menu), select the General tab, and set the Default Error Trapping State option. Visual Basic will use this setting the next time you start it, even if the setting was entered for another project.
Occasionally you may encounter a bug that’s especially difficult to track down. Don’t panic – here are some things that you can do:
For More Information Breakpoints are described in "Using a Breakpoint to Selectively Halt Execution" earlier in this chapter. Read more about Watch expressions in "Monitoring Data with Watch Expressions." The Immediate window is discussed in "Testing Data and Procedures with the Immediate Window." See "Verifying Your Code with Assertions" for more about the Assert method of the Debug object.