DE#9 #8VLQJ#9LVXDO#%DVLF#'HEXJJLQJ#7RROV: 2emhfwlyhv

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

188

/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.

To see a demonstration of the lab solution, click this icon.


(CD-ROM plays the demonstration, "Lab 6 Solution.")

Estimated time to complete this lab: 60 minutes

2EMHFWLYHV
After completing this lab, you will be able to:

• Identify the main features of the Visual Basic debugging tools.


• Use Visual Basic debugging tools to step through an application, examine its variables, change data,
and execute code dynamically.
• Use Visual Basic debugging tools to locate simple logic errors.

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 Open the project and display the debug form


… 1. Open the Debug project located in the <Install Folder>\Labs\Lab06 folder.
… 2. View the form debug.frm.
… 3. On the View menu, point to Toolbars, and click Debug.

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

You can use one of the following methods to set a breakpoint:


y Place your insertion point in the line to break; then, on the Debug menu, click Toggle Breakpoint.
y Place your insertion point in the line to break and press F9.
y Place your insertion point in the line to break, and then click the Toggle Breakpoint button on the
Debug toolbar.
y In the Code Editor window, click the Margin Indicator bar next to the line on which to break.

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

X Run the application and step through the code


… 1. Click the Start button on the toolbar to run the application.
… 2. Move the mouse pointer over the nine buttons and watch the status bar.
The status bar message reminds you which debugging feature each button demonstrates.
… 3. Resize the Visual Basic window so that it takes only half of the screen. This allows you to see both the
Visual Basic window and your application.
… 4. Click the form's Trace button.
The Code Editor window becomes the active window, and the line of code that is about to be executed
is highlighted.
… 5. Click the Step Into button on the Debug toolbar to step through the procedure one line at a time. Watch
the application’s output appear in its window.
… 6. Continue stepping through the procedure until the End Sub statement is highlighted in the Code Editor
window.
… 7. Click the Continue button on the toolbar to resume running the application normally.
… 8. Click the End button to end the program.

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.

X Insert a breakpoint and run the application


… 1. Double-click the Trace With Calls button on the Debug form.

… 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.

X Step into and out of a procedure


… 1. Click the Step Into button twice to step through two lines of code.
The current line contains a call to the PrintRow procedure.
… 2. Click the Step Into button again.
The debugger steps into the PrintRow procedure.
… 3. Click the Step Into button three more times.
The debugger steps into the PrintNumber procedure.
… 4. Click the Step Out Of button to return to the calling procedure.
… 5. On the Run menu, click Continue to resume running the application normally.

X Step over a procedure


… 1. Click the Trace With Calls button in the Debug application again.
… 2. Click Step Over three times.
When the call to PrintRow is highlighted, a Step Over operation runs that procedure and any other
procedures that PrintRow calls, without forcing you to step into them • the debugger steps over the
PrintRow procedure.
… 3. Continue clicking Step Over until the End Sub statement is highlighted.
… 4. Resume running the program normally.
… 5. Click the End button to end the program.

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.

X Examine the values stored in variables


… 1. Double-click the Examine Variables button in the Debug application.
… 2. Set a breakpoint on the first line of executable code in the cmdExamine_Click event procedure.
… 3. Run the application.
… 4. Click the Examine Variables button in the Debug application.
… 5. Step through the procedure until the MsgBox statement is highlighted.
… 6. Switch to the Immediate window.
… 7. Type ? a and press ENTER.
Visual Basic displays the number 1, which is the value currently in the variable a.
… 8. Use the same method to check the values in the variables b, c, d, e, and sum.

X Assign a value to a variable


… 1. Look at the value of the variable sum.
… 2. In the Immediate window, type sum = 20 and press ENTER.
… 3. Type ? sum and press ENTER.
Notice that the value of sum has changed.
… 4. Resume running the program normally.
… 5. Click the End button to end the program.

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.

X Type code in the Immediate window and then run it


… 1. Double-click the Run Code On The Fly button on the Debug form.
… 2. Add the following line of code to the cmdCode_Click event procedure:
Stop

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

Observe that the height of the form changes.


… 7. Type the following lines of code in the Immediate window to show that code can be executed from
Immediate window:
cmdLogic_Click
picOutput.cls
end

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.

X Print directly to the Debug window


… 1. Double-click the Use Debug.Print button on the Debug form.
… 2. Set a breakpoint on the first line of executable code in the cmdDebugPrint_Click event procedure.
… 3. Run the application.
… 4. Click the Use Debug.Print button.
… 5. Step through several lines of code until the Debug.Print statement runs.
The application displays directly to the Immediate window.
… 6. Continue stepping through code and watch the application’s output appear in the Immediate window.
… 7. When you reach the End Sub statement, click the Continue button on the toolbar.
… 8. Click the End button to end the application.

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.

X Examine the calls list


… 1. Double-click the Examine Call Stack button on the Debug form.
… 2. Set a breakpoint on the first line of executable code in the cmdCallStack_Click event procedure.
… 3. Run the application.
… 4. Click the Examine Call Stack button.
… 5. Click the Step Into button nine times until you’re in the most deeply nested procedure call, the
PrintNumber procedure.
… 6. Click the Call Stack button on the toolbar.
Visual Basic displays the Call Stack dialog box, which shows you the trail of calls leading to the
PrintNumber procedure. How many levels deep is the PrintNumber procedure nested?
(CD-ROM displays a hint.)
… 7. Click Close, and then resume normal program execution by clicking Continue on the Run menu.
… 8. Click the End button to end the application.

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.

X Use the Variable Watch button


… 1. Run the application.
… 2. Click the Watch Variables button on the Debug form.
The application displays a message in its own output area, one word per line. The code assigned to this
button extracts the words from a single string.
… 3. Click the End button to end the application.

X Add a watch expression


… 1. Double-click the Watch Variables button in the Debug application.
… 2. In the cmdWatch_Click event procedure, place the insertion point in the variable intWordLength, on
the third line.
… 3. On the Debug menu, click Add Watch.
… 4. In the Expression box, type intWordLength = 6.
… 5. For Watch Type, select the Break When Value Is True option button, and then click OK.
Setting this option tells Visual Basic to stop your code when intWordLength equals 6.
The Watches window appears, displaying the variables being watched.

X Run the application with the watch expression added


… 1. Run the application.
… 2. Click the Watch Variables button in the Debug Application.
The application stops on the second line of the While loop. Notice that four words are displayed in the
application’s output area, none of which are 6 letters long. Also, notice that the value of the expression
in the Watches window is now True.
… 3. Resume running the application normally.
… 4. Click the End button to end the application.

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.

You might also like