CyberiaPC.com Main Page




Learning VB

Debugging your Code (checking for errors)

Imagine you've just completed a project that you've been working on for a long time.  You happily click on the Start button and run your application, however, the program doesn't run as expected.  Calculations give wrong results and other options don't work.  What would you do?  Who you gonna call?  Debug Mode, of course!

Debug Mode is essentially a mode that takes you through your code step by step.  Therefore, you know what exactly happens to each of your variables and procedures.  There are many options that VB gives you when debugging your code, however, this lesson will only show you the main features of this awesome and powerful feature in VB.

There are three main types of errors that you can run into as a programmer: Syntax Errors, Run-Time Errors and Logic Errors.  Syntax Errors happen when you mistype a command, for example, or write an incomplete structure.  VB detects some of these Syntax Errors as you write the code.  Run-Time Errors occur when VB tries carrying out an operation that is impossible, like dividing by zero or calling a non-existent file.  The final type, Logical Errors, is a completely different story.  Everything is compiled properly, with no messages from VB, however, the code doesn't work as expected.  This is because of flaws in the "logic" used in coding.  The only way to fix this type of error is by testing and analyzing your code.

Breakpoints: This is something that you'll want to use a lot.  Breakpoints are markers placed on lines in your code that cause VB to pause as soon as it reaches lines with breakpoints.  This gives you the option to look inside your code and check everything while the application is still active (To check values of variables, hover your mouse cursor over the desired variable).

To set a breakpoint, make sure you're in code view, click on the line where you want VB to break and press F9.  To remove it press F9 again.  The color should automatically change to red.  When you run your program and VB breaks on that line, use the Debug toolbar to step into, out of or over lines of code.  Many other options are also available on the Debug toolbar:

Button Name Description
Start Runs the application from the startup form (or Sub Main) specified on the General tab of the Project Properties dialog box.  If in Break mode, the Start button's tooltip text changes to Continue.
Break Stops execution of a program temporarily. Click the Continue button to resume running the program.
End Stops running the program and returns to Design mode.
Toggle Breakpoint Creates or removes a breakpoint. A breakpoint is a place in the code where Visual Basic automatically halts execution and enters Break mode.
Step Into Runs the next executable line of code, stepping through each line of code that follows. If the code calls another procedure, Step Into steps through each line in that procedure also.
Step Over Runs the next executable line of code, stepping through each line of code that follows. If the code calls another procedure, that procedure runs completely before stepping to the next line of code in the first procedure.
Step Out Executes the remainder of the current procedure and breaks at the next line in the calling procedure.
Locals Window Displays the current value of local variables.
Immediate Window Displays the Immediate window, if it is not displayed. The Immediate window allows you to execute code or query values while the application is in Break mode.
Watch Window Displays the Watch window, if it is not displayed. The Watch window displays the values of selected expressions.
Quick Watch Displays the current value of the expression the cursor is on while in Break mode. The expression can easily be added to the Watch window.
Call Stack Displays the Call Stack dialog box if it is not displayed. While in Break mode, the Call Stack button lists the currently active procedure calls by presenting a dialog box that shows all procedures that have been called but not yet run to completion.
Info in above table from Microsoft

Quick Watch will be discussed here, the other options on the Debug toolbar will not be discussed, however, you may find that you'll get used to how they work by practice.

Quick Watch allows you to check the value of a property, variable or expression during Debug Mode.  To add a Quick Watch to an expression, select it while in Code View and click on the Quick Watch button on the Debug Toolbar.  A dialogue will appear showing you the value of the expression.  If you want to add the expression to the Watch window, then click on the Add button.

{Go Back To VB Lessons Page}