Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 8 Debugging Visual Basic Programs 217



  1. Click the Step Into button twice more.


The If statement evaluates the conditional expression to False, and the compiler moves
to the Else statement in the decision structure. Here’s our bug—the decision structure
logic is incorrect because a 13-year-old is a teenager. Do you recognize the problem?
The first comparison needs the greater than or equal to (>=) operator to specifically
test for this boundary case of 13. You’ll stop debugging now so that you can fix this
logic error.


  1. Click the Stop Debugging button on the Standard toolbar.

  2. In the Code Editor, add the equal to sign (=) to the first condition in the If statement so
    that it reads:


If Age >= 13 And Age < 20 Then


  1. Run the program again and test your solution, paying particular attention to the
    numbers 12, 13, 19, and 20—the boundary, or “fringe,” cases that are likely to cause
    problems.
    Remember that you still have a breakpoint set, so you’ll enter debugging mode
    when you run the program again. Use the Step Into button to watch the program
    flow around the crucial If statement, and use the Autos window to track the value
    of your variables as you complete the tests. When the form opens, enter a new
    value and try the test again. (You’ll learn how to remove the breakpoint later in
    the chapter .)

  2. When you’re finished experimenting with debugging mode, click the Stop Debugging
    button on the Standard toolbar to end the program.
    Congratulations! You’ve successfully used debugging mode to find and correct a logic error
    in a program.


Tracking Variables by Using a Watch Window


The Autos window is useful for examining the state of certain variables and properties as
they’re evaluated by the compiler, but items in the Autos window persist, or maintain their
values, only for the current statement (the statement highlighted in the debugger) and the
previous statement (the statement just executed). When your program goes on to execute
code that doesn’t use the variables, they disappear from the Autos window.

To view the contents of variables and properties throughout the execution of a program,
you need to use a Watch window, a special Visual Studio tool that tracks important
values for you so long as you’re working in debugging mode. In Visual Studio, you can
Free download pdf