Flylib.com

Books Software

 
 
 

Section 11.3


[Page 626 ( continued )]

Section 11.3

VB2005's classes have full object-oriented capability, including inheritance, polymorphism, and abstraction.



[Page 627]

Appendix D. Visual Basic Debugging Tools

Errors in programs are called bugs , and the process of finding and correcting them is called debugging . Since the Visual Basic editor does not discover errors due to faulty logic, these errors present the most difficulties in debugging. One method of discovering a logical error is by desk-checking , that is, tracing the values of variables on paper by writing down their expected value after "mentally executing" each line in the program. Desk checking is rudimentary and highly impractical except for small programs.

Another method of debugging involves placing MsgBox statements at strategic points in the program and displaying the values of selected variables or expressions until the error is detected . After correcting the error, the MsgBox statements are removed. For many programming environments, desk checking and MsgBox statements are the only debugging methods available to the programmer.

The Visual Basic debugger offers an alternative to desk checking and MsgBox statements. It allows you to pause during the execution of your program in order to view and alter values of variables. These values can be accessed through the Immediate, Watch, and Locals windows , known as debugging windows .



[Page 627 ( continued )]

The Three Program Modes

At any time, a program is in one of three modes design mode , run mode , or debug mode . (Debug mode is also known as break mode .) When the current mode is "run" or "debug," the words "Running" or "Debugging" are displayed (in parentheses) in the Visual Basic title bar. The absence of these words indicates that the current mode is "design."


[Page 628]
(This item is displayed on page 627 in the print version)

Title bar and toolbars during design mode.

Title bar and toolbars during run mode.

Title bar and toolbars during debug mode.


With the program in design mode, you place controls on a form, set their initial properties, and write code. Run mode is initiated by pressing F5 or the Start button. Debug mode is invoked automatically when a run-time error occurs. You can use Debug options to break the program at specified places. While the program is in debug mode, you can hover the cursor over any variable to obtain its current value. Also, you can use the debugging windows , such as the Immediate, Watch, and Locals windows , to examine values of expressions. When you enter debug mode, the Toolbar contains a Continue button. You can click on it to continue with the execution of the program.



[Page 628 ( continued )]

Stepping through a Program

The program can be executed one statement at a time, with each press of an appropriate function key executing a statement. This process is called stepping . After each step, values of variables , expressions, and conditions can be displayed in the debugging windows , and the values of variables can be changed.

When a procedure is called, the lines of the procedure can be executed one at a time, referred to as stepping into the procedure, or the entire procedure can be executed at once, referred to as stepping over a procedure. A step over a procedure is called a procedure step . In addition, you can execute the remainder of the current procedure at once, referred to as stepping out of the procedure. The three toolbar buttons shown in Figure D.1 can be used for stepping.

Figure D.1. The toolbar buttons used to Step Into, Step Over, and Step Out.


As another debugging tool, Visual Basic allows the programmer to specify certain lines as breakpoints . Then, when the program is run, execution will stop at the first breakpoint reached. The programmer can then either step through the program or continue execution to the next breakpoint. Also, the programmer can place the cursor on any line in the program and have execution stop at that line with a "Run to Cursor" command. Program execution normally proceeds in order through the statements in the procedure. However, at any time the programmer can specify the next statement to be executed.

The tasks discussed previously are summarized next, along with a means to carry out each task. The tasks invoked with function keys also can be produced from the menu bar, the context menu (produced by clicking the right mouse button), or the toolbar.


[Page 629]

Run to cursor

Press Ctrl + F8

Step Into

Press F8

Step Over

Press Shift + F8

Step Out

Press Ctrl + Shift + F8

Set a breakpoint

Move cursor to line, press F9

Remove a breakpoint

Move cursor to line containing breakpoint, press F9

Clear all breakpoints

Press Ctrl + Shift + F9

Continue execution to next

 

breakpoint or the end of the program

Press F5

Stop debugging

Ctrl + Alt + Break