30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1304 <strong>Visual</strong> Studio .<strong>NET</strong> Debugger Appendix D<br />

Testing and Debugging Tip D.2<br />

After fixing one error, recompile your program. You may observe that the number of overall<br />

errors perceived by the compiler is significantly reduced. 24.0<br />

Debugging is the process of finding and correcting logic errors in applications. Logic<br />

errors are more subtle than syntax errors because the program compiles successfully, but<br />

does not run as expected. Logic errors are often difficult <strong>to</strong> debug because the programmer<br />

cannot see the code as it is executing. Some programmers attempt <strong>to</strong> debug programs using<br />

message boxes or Console.WriteLine statements. For example, the programmer<br />

might print the value of a variable when the variable’s value changes <strong>to</strong> determine if it is<br />

being set correctly. This method is cumbersome, because programmers must write a line of<br />

code wherever they suspect may be a problem. Once the program has been debugged, the<br />

programmer must remove these printing statements.<br />

Debuggers provide a set of <strong>to</strong>ols that allow the programmer <strong>to</strong> analyze a program while<br />

it is running. These <strong>to</strong>ols allow the programmer <strong>to</strong> suspend program execution, examine and<br />

set variables, call procedures without having <strong>to</strong> modify the program and much more. In this<br />

appendix, we introduce the <strong>Visual</strong> Studio .<strong>NET</strong> debugger and several of its debugging <strong>to</strong>ols.<br />

[Note: A program must successfully compile before it can be used in the debugger.]<br />

D.2 Breakpoints<br />

Breakpoints are a simple but powerful debugging <strong>to</strong>ol. A breakpoint is a marker that can be<br />

set at any executable line of code. When a program reaches a breakpoint, execution pauses,<br />

allowing the programmer <strong>to</strong> examine the state of the program and ensure that everything is<br />

working properly. We use the following program (Fig. D.2) <strong>to</strong> demonstrate debugging a<br />

loop using the features of the <strong>Visual</strong> Studio .<strong>NET</strong> debugger. This program is designed <strong>to</strong><br />

output the value of ten fac<strong>to</strong>rial (10!), but contains two logic errors—the first iteration of<br />

the loop multiplies x by 10 instead of 9, and the result of the fac<strong>to</strong>rial calculation 0.<br />

1 ' Fig. D.2: DebugExample.vb<br />

2 ' Sample program <strong>to</strong> debug.<br />

3<br />

4 Module modDebug<br />

5<br />

6 Sub Main()<br />

7 Dim x As Integer = 10<br />

8 Dim i As Integer<br />

9<br />

10 Console.Write("The value of " & x & " fac<strong>to</strong>rial is: ")<br />

11<br />

12 ' loop <strong>to</strong> determine x fac<strong>to</strong>rial, contains logic error<br />

13 For i = x To 0 Step -1<br />

14 x *= i<br />

15 Next<br />

16<br />

17 Console.WriteLine(x)<br />

18 End Sub ' Main<br />

19<br />

20 End Module ' modDebug<br />

Fig. D.2 Debug sample program (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!