DE#9 #8VLQJ#9LVXDO#%DVLF#'HEXJJLQJ#7RROV: 2emhfwlyhv
DE#9 #8VLQJ#9LVXDO#%DVLF#'HEXJJLQJ#7RROV: 2emhfwlyhv
DE#9 #8VLQJ#9LVXDO#%DVLF#'HEXJJLQJ#7RROV: 2emhfwlyhv
/DE#9=#8VLQJ#9LVXDO#%DVLF#'HEXJJLQJ#7RROV
In this lab, you will explore the Visual Basic debugging tools and see the different types of assistance
these features can provide when debugging a program.
You will use a project provided for you, named Debug, located in the <install folder>\Labs\Lab06 folder.
2EMHFWLYHV
After completing this lab, you will be able to:
To complete the exercises in this lab, you must have the required software. For detailed information about
the labs and setup for the labs, see Labs in this course.
3UHUHTXLVLWHV
There are no prerequisites for this lab.
([HUFLVHV
The following exercises provide practice working with the concepts and techniques covered in chapter 6.
([HUFLVH#4=#6HWWLQJ#%UHDNSRLQWV#DQG#6WHSSLQJ#7KURXJK#&RGH
In this exercise, you will trace the logic of a procedure by stepping through its Visual Basic code one line at
a time.
([HUFLVH#5=#6WHSSLQJ#,QWR#DQG#6WHSSLQJ#2YHU#3URFHGXUHV
In this exercise, you will see the differences between stepping into, over, and out of a procedure. To step
into a procedure means to run it one line at time. To step over a procedure means to run it as a unit. To
step out of a procedure means to run it until it completes the current procedure and returns to the calling
procedure.
([HUFLVH#6=#([DPLQLQJ#9DULDEOHV#LQ#WKH#,PPHGLDWH#:LQGRZ
In this exercise, you will use the Immediate window to check the values stored in variables while your code
is running. You will also change the values of variables from within the Immediate window.
([HUFLVH#7=#5XQQLQJ#&RGH#IURP#WKH#,PPHGLDWH#:LQGRZ
In this exercise, after clearing all of the breakpoints that you have added to the Debug project, you will run
code from the Immediate window.
([HUFLVH#8=#2XWSXWWLQJ#7H[W#WR#WKH#'HEXJ#:LQGRZ
In this exercise, you will use the Debug.Print statement (the Print method of the Debug object) to display
text in the Immediate window. This technique is helpful if you know of a particular place in your code
where a variable changes repeatedly and you need to observe those changes. It is also helpful in places
where calculations occur and you can’t print the results of those calculations directly to a form.
Lab 6: Using Visual Basic Debugging Tools
([HUFLVH#9=#([DPLQLQJ#WKH#&DOOV#/LVW
In this exercise, you will trace the flow of your code by examining the stack of procedure calls in the Call
Stack dialog box.
([HUFLVH#:=#0RQLWRULQJ#9DULDEOHV#ZLWK#:DWFK#([SUHVVLRQV
In this exercise, you will add a watch expression to test the value of a variable named intWordLength, and
break when intWordLength equals 6.
([HUFLVH#;=#)LQGLQJ#D#/RJLF#(UURU#RQ#<RXU#2ZQ
In this exercise, you will use the skills you have learned to find and fix a logic error by yourself.
Notes
Page 36
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#4=#6HWWLQJ#%UHDNSRLQWV#DQG#6WHSSLQJ#7KURXJK#&RGH
In this exercise, you will trace the logic of a procedure by stepping through its Visual Basic code one line at
a time.
X Insert a breakpoint
… 1. Double-click the Trace button on frmDebug to view the cmdLogic_Click event procedure in the Code
Editor window.
… 2. In the cmdLogic_Click event procedure, set a breakpoint on the first line of executable code:
picOutput.Cls
Note Variable declarations are not executable statements. If you press F9 when the insertion point is in
a declaration statement, Visual Basic displays an error message.
By default, the line’s background color changes to red, and a red dot appears on the Margin Indicator
bar in the Code Editor window. When you run the code, Visual Basic will stop at this line, and the
program will switch to Break mode.
All of these methods toggle breakpoints. If any of them are repeated, the breakpoint will be deleted and
the line will change back to its normal color.
Notes
Page 37
Lab 6: Using Visual Basic Debugging Tools
Notes
Page 38
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#5=#6WHSSLQJ#,QWR#DQG#6WHSSLQJ#2YHU#3URFHGXUHV
In this exercise, you will see the difference between stepping into, over, and out of a procedure. To step
into a procedure means to run it one line at time. To step over a procedure means to run it as a unit. To
step out of a procedure means to run it until it completes the current procedure and returns to the calling
procedure.
… 2. Set a breakpoint on the first line of executable code in the cmdLogicCalls_Click event procedure.
… 3. Run the application.
… 4. Click the Trace With Calls button.
The code does the same thing it did in the previous exercise, but this time it is broken into separate
procedures instead of doing everything in a pair of nested For loops.
Notes
Page 39
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#6=#([DPLQLQJ#9DULDEOHV#LQ#WKH#,PPHGLDWH#:LQGRZ
In this exercise, you will use the Immediate window to check the values stored in variables while your code
is running. You will also change the values of variables from within the Immediate window.
Notes
Page 40
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#7=#5XQQLQJ#&RGH#IURP#WKH#,PPHGLDWH#:LQGRZ
In this exercise, after clearing all of the breakpoints that you have added to the Debug project, you will run
code from the Immediate window.
X Clear breakpoints
• On the Debug menu, click Clear All Breakpoints.
When Visual Basic executes this line, it will enter Break mode.
… 3. Run the application.
… 4. Click the Run Code On The Fly button in the Debug window.
… 5. Type the following lines of code in the Immediate window to show that expressions can be evaluated
and statements can be executed in the Immediate window:
x = sqr(2.0)
? x
x = x + 1
? x
… 6. Type the following lines of code in the Immediate window to show that properties can be queried or set
in the Immediate window:
? me.height
me.height = me.height + 100
? cmdCode.caption
Notes
Page 41
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#8=#2XWSXWWLQJ#7H[W#WR#WKH#'HEXJ#:LQGRZ
In this exercise, you will use the Debug.Print statement (the Print method of the Debug object) to display
text in the Immediate window. This technique is helpful if you know of a particular place in your code
where a variable changes repeatedly and you need to observe those changes. It is also helpful in places
where calculations occur and you can’t print the results of those calculations directly to a form.
Notes
Page 42
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#9=#([DPLQLQJ#WKH#&DOOV#/LVW
In this exercise, you will trace the flow of your code by examining the stack of procedure calls in the Call
Stack dialog box.
Notes
Page 43
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#:=#0RQLWRULQJ#9DULDEOHV#ZLWK#:DWFK#([SUHVVLRQV
In this exercise, you will add a watch expression to test the value of a variable named intWordLength, and
break when intWordLength equals 6.
Notes
Page 44
Lab 6: Using Visual Basic Debugging Tools
188 ([HUFLVH#;=#)LQGLQJ#D#/RJLF#(UURU#2Q#<RXU#2ZQ
In this exercise, you will use the skills you have learned to find and fix a logic error by yourself.
X Run the code with the logic error and then fix the error
… 1. Run the application.
… 2. Click the Trace button in the Debug application and watch the output in its window. This is the correct
output.
… 3. Click the Fix Logic Error button in the Debug application.
Only two columns of output appear, not four.
… 4. Using your knowledge of the Visual Basic debugging tools, find the error and fix it.
To see additional information to help you complete the exercise, click this icon.
(CD-ROM displays a hint.)
Notes
Page 45
This page intentionally left blank.