• We now consider the search for, and elimination of, logic errors. These
are errors that don’t prevent an application from running, but cause incorrect
or unexpected results. Visual
Basic provides an excellent set of debugging tools to aid in this search.
• Debugging a code is an art, not a science. There are no prescribed
processes that you can follow to eliminate all logic errors in your program. The
usual approach is to eliminate them as they are discovered.
• What we’ll do here is present the debugging tools available
in the Visual
Basic environment (several of which appear as buttons on the toolbar) and
describe their use with an example. You, as the program designer, should select
the debugging approach and tools you feel most comfortable with.
• The interface between your application and the debugging tools is
via three different debug windows: the Immediate Window, the Locals Window, and
the Watch Window. These windows can be accessed from the View menu (the Immediate
Window can be accessed by pressing Ctrl+G). Or, they can be selected from the
Debug Toolbar (accessed using the Toolbars option under the View menu):
• All debugging using the debug windows is done when your application
is in break mode. You can enter break mode by setting breakpoints, pressing Ctrl+Break,
or the program will go into break mode if it encounters an untrapped error or
a Stop statement.
• Once in break mode, the debug windows and other tools can be used
Determine values of variables
Set watch variables and expressions
Manually control the application
Determine which procedures have been called
Change the values of variables and properties
Example - Debugging
1. Unlike other examples, we’ll do this one as a group. It will be used
to demonstrate use of the debugging tools.
2. The example simply has a form with a single command button. The button is
used to execute some code. We won’t be real careful about proper naming
conventions and such in this example.
3. The code attached to this button’s Click event is a simple loop that
evaluates a function at several values.
Private Sub Command1_Click()
Dim X As Integer, Y As Integer
X = 0
Y = Fcn(X)
X = X + 1
Loop While X <= 20
This code begins with an X value of 0 and computes the Y value using the general
integer function Fcn. It then increments X by 1 and repeats the Loop. It continues
looping While X is less than or equal to 20. The function Fcn is computed using:
Function Fcn(X As Integer) As Integer
Fcn = CInt(0.1 * X ^ 2)
Admittedly, this code doesn’t do much, especially without any output,
but it makes a good example for looking at debugger use. Set up the application
and get ready to try debugging.