Visual Basic 2005 Express provides you with a number of tools for tracking down and dealing with errors. Using these tools, you can track down errors by monitoring your program code statements as they execute in order to examine variable and property values and to observe the execution flow of your application as it moves from statement to statement.
Up to this point in the book, you have executed your Visual Basic applications mostly in design mode by pressing the F5 key within the Visual Basic IDE. However, you also learned how to create standalone executable versions of your Visual Basic applications, known as run-time mode. Now it's time to learn about a third mode of application execution, break mode.
In break mode, you are able to pause the execution of your application and examine its status before resuming execution. This can be a very effective tool for tracking down and locating statements that may be incorrectly setting variable and property values. Break mode allows you to step through each line of your program code, to see what procedures have been called, and to examine variable and property values.
To set up a breakpoint within your application, click on the left-hand margin of the code editor on the line where you wish to place the breakpoint. Once the breakpoint has been set, a circular marker is displayed in the margin, and the line of code where the breakpoint is set is highlighted, as demonstrated in Figure 11.6.
Figure 11.6: Using a breakpoint to pause an application's execution.
Place your breakpoints at locations within your applications where you suspect a problem may be occurring. You can set as many breakpoints as you wish. When you press F5 to run your application and a breakpoint is reached, program execution pauses and the circular marker for the current breakpoint changes to display an arrow, as demonstrated in Figure 11.7.
Figure 11.7: The Visual Basic IDE changes the highlight of the currently active breakpoint.
Trick | Once you have resolved any errors that you may have and no Longer need any breakpoints, you can remove them by clicking on the circular markers Located in the left-hand margin. |
You can learn a lot about what is going on in your application by setting a breakpoint and then examining the value of properties and variables to see what they have been set to. For example, at the bottom of Figure 11.7, you can see that the Locals window and the Immediate Window have been opened. The Locals window displays information about variables within the current context, including their current value and type. If you want, you can even change the value assigned to a variable displayed in the Locals window.
You can also use the Immediate Window to query the value of a variable or property. For example, if you look at the Immediate Window in Figure 11.7, you will see where I checked on the value assigned to the intUpperLimit variable by typing in a question mark followed by the name of the variable.
By using breakpoints and the windows mentioned above, you can track down many errors by locating the statements in your applications that inappropriately modify variable or property values.
Another important feature provided by Visual Basic when executing an application in break mode is the ability to step through program statements. This allows you to identify a section of code where you think an error may be located (by setting a breakpoint) and then to step through and execute each subsequent statement a line at a time. This lets you follow the execution flow of an entire process, pausing at any point along the way to check on the value of related variables and properties.
Visual Basic provides you with three different ways to step through your program statements, as listed below:
Step Into. Executes the breakpoint statement and then pauses before the execution of statements that follow
Step Over. Executes entire procedures, pausing before the execution of the next procedure
Step Out. Used inside a procedure in order to return to the calling procedure where program flow is then paused
You can selectively choose which of these step options you wish to use at any moment by clicking on their icons, which are located in the Visual Basic standard toolbar, as shown in Figure 11.8.
Figure 11.8: Using the Debug icons on the standard toolbar to step through program statements.