0% found this document useful (0 votes)
116 views14 pages

3.1.6 The VB Debugger - 1 PDF

Uploaded by

Lara Arinelli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views14 pages

3.1.6 The VB Debugger - 1 PDF

Uploaded by

Lara Arinelli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

The VB Debugger 1

The VB Debugger

© 2001 Hyprotech Ltd. - All Rights Reserved. 1

3.1.6 The VB Debugger_1.pdf


2 The VB Debugger

Introduction
As you may have already experienced, it is quite easy to make an error
while writing VB programs. Virtually all errors, however minute, will
cause the program to function improperly or to fail completely.
Programming is a demanding task; often anything less than perfection
is not acceptable.

Once errors are made, it can be difficult to find them; this is because the
error will often cause the program to fail without giving any reason.
Fortunately, VB comes with an indispensable tool for locating program
errors. This tool is called the VB Debugger.

In this module, the VB Debugger is introduced. Its functions are


highlighted, and an exercise is provided to help you develop the ability
and knowledge needed to successfully use this powerful tool.

Learning Objectives
By completing this module, you will learn how to use the VB Debugger
to effectively remove programming errors from a VB program.

Prerequisites
Before beginning this module, you should have a solid grasp of:

• VB programming structure
• VB syntax

2
The VB Debugger 3

The VB Debugger
The VB Debugger is a useful tool that helps the programmer find (and
correct) program errors. Without this tool, finding errors in a VB
program would be much more difficult.

As code is written in VB, the compiler checks each line for proper
syntax. If the syntax is incorrect, or incomplete, an error box will appear
advising the programmer of the error and suggesting a method of
correction. For example, typing Set objVar = and then pressing the
<Enter> key will bring up an error box that will tell you that VB expects
an object to be placed after the "=" in this line. This is called a Syntax
Error.

If, however, an object that does not exist is placed after the "=" in the
example presented above, no error box will be generated after pressing
the <Enter> key. Rather, the error will occur when the program is run.
This is because the syntax of the line is correct, but a reference to a
fictitious object can not be created. This is called a Run-Time Error.

Run-time errors are the hardest errors to find, and they are also the
errors that cause the most problems for a running program. Typically,
run-time errors will cause the VB program to crash, or to stop
functioning. An error handler can be added to the program to specify
the program’s response to a run-time error. An error handler is a line of
code, usually located at the beginning of the program that begins with
On Error .... Examples of error handlers can be found in the programs
that were developed in the previous module.

Since run-time errors may not be detected by the compiler like syntax
errors, they can be difficult to locate. The VB Debugger was developed
to help with the task of finding and removing these types of errors.

3
4 The VB Debugger

Adding Breakpoints
Run-time errors are often related to the logic or setup of the code. The
code may have the proper syntax, but the values that it produces may
be far from those expected. Error messages may help guide you to the
area in the program that is causing the errors; however, more
investigation is often necessary to determine the cause of run-time
errors.

Breakpoints can be added to a VB program to allow inspection of values


at specific points in the code. These breakpoints allow the user to
monitor the progress of the running program.

Breakpoints can be added in several ways. First, select the desired


Breakpoints cannot be placed
on comment lines or variable position of the breakpoint. Typically, this will be 2 or 3 lines above the
declaration lines. suspected trouble spot. Note that breakpoints can only be placed on
lines of executable code. Variable declarations and comment lines are
not considered executable code. Once the position is selected, choose
one of the following methods to activate the breakpoint at that
position.

• Select Toggle Breakpoint from the Debug menu in the main


menu bar.
• Press the <F9> hot key.
• Click in the grey column on the left side of the code window
beside the desired breakpoint location.

Once the breakpoint is activated, the line will be highlighted in red and
a red dot will appear in the grey column on the left. When the program
is running, and it encounters a breakpoint, the execution is halted and
the program enters "break" mode. There are four typical actions that
the user can perform while the program is in break mode. These four
actions can be found in the Debug menu in the main menu bar as
follows:

These four actions will be defined here:

• Step Into - allows the user to execute the code line-by-line,


entering subroutines, functions, as well as, other modules and
classes, etc.
• Step Over - allows the user to "step over" a section of code like
a subroutine or function call.

4
The VB Debugger 5

• Step Out - allows the user to exit a looping statement before it


has completed each pass of its execution.
• Run to Cursor - allows the program to complete all of the code
between the current location and the location of the cursor.
This action is especially useful when debugging programs with
numerous loops, the loop can be executed quickly by placing
the cursor immediately after the loop and selecting this action.

Alternatively, after a breakpoint is encountered, the program can be run


normally by pressing the Run/Continue button.This will cause the
The Run/Continue button program to run though the code until the next breakpoint is hit (if
applicable), or to the end of the code.

The Debugger Windows


Activating a breakpoint and running the code line-by-line should locate
the source of the error. To understand why the error is occurring, it may
be necessary to work with the more advanced features of the VB
Debugger. The Debugger has three panes that allow for user interaction
with the code, and the debugger.

The three panes (or windows) are:

• Immediate Window - displays information coming from the


Debug object during run time. Can also be used to evaluate
expressions and variables at any given point in time or to set
the value of variables while in debugging mode.
• Locals Window - automatically displays the name, value, and
type of all of the local variables in the current subroutine or
function. The user can also edit the variable’s value in this
window.
• Watch Window - appears whenever "watch" expressions are
created. Displays the same properties as the Locals window,
but only for variables that have been added to it.

5
6 The VB Debugger

The Immediate Window


To display the Immediate window, choose Immediate Window from
the View menu in the main menu bar, or press the <Crtl><G> hot key.
Individual lines of code can be run in the Immediate window just as if
they were in the main body of code. The various applications of the
Immediate window include the following:

• Determine, or change, the value of a variable while the


execution of the code is halted, i.e. the program is in Break
mode.
• Call Subroutine and Functions as you would in the code.
• Test suspicious or newly written code before adding it to the
code base.
• View debugging output while the program is running.

This last application is one of the most common uses for this window.
The VB Debugger can print various types of information in the
Immediate window. Including a line like the following in the Error
handler will result in a brief description of the error being displayed in
the Immediate window.

Debug.Print Err.Description

Note that code written in the Immediate window is executed in context.


That means that it is run as if it were entered in the current module that
is running.

The Locals Window


As mentioned previously, the Locals window is used in monitor all local
variables in the current module. This window is accessed by selecting
Locals Window from the View menu in the main menu bar.

There are three columns in this window:

• Expression - displays the name of the variable.


• Value - displays the value of the variable. You can edit the
value that is shown in this column.
• Type - displays the variable’s type.

6
The VB Debugger 7

The Watch Window


The Watch window is used to monitor all watches that are active in the
VB program. Watches are used to monitor the value of Variables,
Functions, or VB expressions.

Watches are added by selecting Add Watch from the Debug menu in the
main menu bar, or by selecting Add Watch from the menu that appears
when the code window is "right-clicked". The Add Watch Dialog
window looks like this:

The desired expression is entered into the area as indicated, the context
is chosen using the drop-down lists, and the watch type is selected.
Once all of the information is entered, pressing the OK button will add
the watch to the Watch window.

Once a watch expression is added to the Watch window, VB will


monitor the status of the expression and will update the Watch window
accordingly.

The Call Stack


While the VB program is in "break" mode, it is possible to see a list of all
Subroutines, Functions, and Properties that the module has called but
not completed, up until the break point.

This can be used to trace the subroutine and/or function calls that
caused a certain piece of code to be executed.

The Call Stack can be accessed by selecting Call Stack from the Debug
menu in the main menu bar, or by pressing the <Ctrl><L> hot key.

7
8 The VB Debugger

Workshop
This workshop is designed to help you develop your debugging skills.
The information and techniques presented in the last several pages will
be used here to debug a VB program that has a total of nine errors. The
instructor will work through the first few problems with you and
demonstrate the various debugging tools. The remaining errors will be
left as an exercise for you to complete if time permits.

There are three controls that will interact with the VB program. The
"Start HYSYS" button will complete the link to HYSYS and open the
selected case. The "Stream Combo Box" is used to select the desired
stream. Finally, the "Load Conditions" button will move the user
specified values back into the HYSYS case and then update the values
in the spreadsheet. The following flowchart highlights the relationship
between the various controls and the subroutines.

8
The VB Debugger 9

Debugging the Program


The following steps will guide you through the debugging process. The
instructor will work with you to remove the first few errors and will let
you work on the remaining errors individually.

1. Open the Excel workbook called Workshop_Debug.xls located on


the Debug_Starter directory of the course disk. The following
worksheet should be displayed.

2. Press the Start HYSYS button and note that an error appears. The
error box should look like this:

It is recommended that you


save the program after
3. This message box tells you that a "Compile Error" has occurred
successfully removing each because "User Type not defined". Translated, this means that the
error. VB compiler does not know what a HYSYS.Application object is.
Stop the program and correct this error. Save your program.

What did you do to correct this error? _______________

9
10 The VB Debugger

4. Return to the Excel workbook and press the Start HYSYS button
again. This time a Message Box appears that reads "Could not
load file." Return to the code view and try to spot the location of
the error. Finding an error of this type can be very difficult
without using breakpoints and stepping through the code.
5. Place a breakpoint on the line that begins On Error.... within the
StartHYSYS subroutine. We know that this line works because the
Message box displayed properly. Return to the workbook and
press the Start HYSYS button. This time the code will run
normally until the breakpoint is encountered, then it will enter
the "break" mode.
6. Notice that the line that contained the break point is highlighted
in yellow. This means that it is the current line. Press <F8> to run
this line, and notice that the yellow highlight moves to the next
line.
7. Press <F8> a few more times to move through the code. Notice,
however, that the yellow highlight skips right to the Error
Handler, so we know that the SimCase = ... line is causing the
error.
8. With the yellow highlight on the Debug.Print ... press the <F8>
hot key and then open the Immediate window (<Ctrl><G>).

What error description does the Immediate window


contain?________________________________
What is the object variable in this line? _____________

The Stop/Reset button


9. Press the Stop/Reset button to allow correction of this error. Fix
the error and return to the workbook. Press the Start HYSYS
button again; the program will stop execution at your breakpoint.
Press <F8> to step through the code.

Did you correctly fix the problem in the SimCase... line?


__________
If so, save your program!

10. Continue to step through the program, select the HYSYS Case
Workshop_Debug.hsc in the Debug_Starter directory when
prompted. Eventually, you will enter the UpdateComboBox
subroutine, and an error will occur on the next line.

What error description is given here? _______________


What type of variable is "streamNames"? _____________
What type of variable is on the right of the "="? ___________

10
The VB Debugger 11

11. Fix this error and reset the program; i.e. press the Stop/Reset
button. Save your program.
12. Press the Start HYSYS button once more and step though the
program past the location of error #3. Continue to step through
the program until another error comes up.

Have we seen this error message before? ____________


Why did it appear again? Is the HYSYS case not properly
loaded? _______________

13. Fix this error and save the program. Press the Start HYSYS button
again. All errors in the various subroutines that this command
calls have been fixed; therefore, we can move onto the two other
commands.
14. Choose a stream from the drop-down list. Another compile error
will appear that reads "Type Mismatch". The
"VapourFractionValue" term is highlighted in the code base, so
this is a good place to start looking for the error. Inspect the
"GetStreamValue" subroutine.

RV is declared as what type of variable in the subroutine


definition? _______________
What type of variable does "VapourFractionValue" return?
____________

15. Fix this error and save your program. Reset the program, press the
Start HYSYS button, and select another stream from the box. A
message box will appear that reads "Error in Retrieving stream
data."

Which subroutine or function contains the error handler


that will produce this type of error message? ___________

16. Add a breakpoint to the top of this subroutine and step through
the code to find the error. Remember to reset the program and to
press the Start HYSYS button to reset the link.

Which line of code causes the error? _______________


Why? _____________

Save your Program!

11
12 The VB Debugger

17. Remove the breakpoint added in step 16. Press the Start HYSYS
button and select a stream from the box. An error box will appear
that reads "Error Retrieving Mass Flow." Add a breakpoint to the
GetStreamData subroutine on the line which retrieves the mass
flow. The description of the error that VB gives is "Method ’Get
Value’ of object ’RealVariable’ failed."

Why did this method fail? __________


Was RV not set properly? ___________
Is it a problem with the method’s argument? ____________

18. Remove the breakpoint from step 17, restart HYSYS and select a
stream. A new error box should appear that reads "Error
Retrieving Compositions". Add a breakpoint to the subroutine
that is causing the error. Notice that the error occurs in the For i =
0 to comps.count loop. The error is described as "Subscript out of
range."

What is wrong with this loop? _____________


How many times does it iterate? _______________
How many components are there? _____________

19. The code within the cbxSource_Change event should now be


completely debugged. Clear all breakpoints, restart the program,
and select a stream. This event will be debugged if the entire
worksheet is completed without any error messages.

Does the program work as expected? _____________


Are all of the errors removed? ___________

20. Change one of the blue values and press the Load Conditions
button. A number of errors should appear. The error messages
should start with "Error in Setting...." Add a breakpoint to find the
source of the errors. The VB description of the error is
"Permission Denied"

What line causes the error? _______________


Why is "permission denied" to change this variable?
__________
How can you fix this error? _______________

12
The VB Debugger 13

21. Fix this error, and save your program. Test your solution by
restarting the program, selecting a stream, and changing a value.

Does the program work as it should? ____________


Have all the errors been removed? _____________

Save your Program!

13
14 The VB Debugger

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy