1. Computing

Visual Basic .NET 2010 Express - What's New With Visual Basic .NET Express

Debugging with VB.NET 2010


Debugging is a skill you just keep improving for as long as you keep coding. You can get some incredibly sophisticated tools available to help you debug, but nearly all the time, you can find the answer faster with just a few simple techniques and your own intelligence. We're going to write a little code, create a bug, and find it using the simple tools and intelligence. In the process, we'll learn the basics of debugging a VB.NET program.

First, start a new project and open the Form Load event subroutine. (Note that I'm not detailing every step now.) Add this code to the subroutine. The calculation is just some random thing I invented. It doesn't mean anything and it's just there to generate a decimal number.

Dim myDecimalArray(10) As Decimal
For i As Short = 1 To 10
    myDecimalArray(i) = ((i ^ 3) + i) / 7
    Console.WriteLine("For-Next loop: " &

When you run the project, the Output window (Debug menu > Windows > Output) displays this result:

For-Next loop: 0.285714285714286
For-Next loop: 1.42857142857143
For-Next loop: 4.28571428571429
For-Next loop: 9.71428571428571
For-Next loop: 18.5714285714286
For-Next loop: 31.7142857142857
For-Next loop: 50
For-Next loop: 74.2857142857143
For-Next loop: 105.428571428571
For-Next loop: 144.285714285714

Inserting statements to display values while a program is running is the oldest and one of the easiest ways to debug. The Output window will refresh every time you run the program. If you want the values to be saved between executions, use Debug.WriteLine instead and look in the Immediate window.

But VB.NET has much better techniques!

You can also put a breakpoint on the Next statement. When you run the program, execution will halt just before that statement is executed. Then you can select whatever values you want and display them using Quickwatch. Or, new in VB.NET 2010, you can "pin" the display of the value to the code window and even add a comment. The illustration below shows the extensive menu of things you can do at a breakpoint. (The Run, Stop, and Stepping icons are also circled. I refer to these in a few paragraphs.)

Click Here to display the illustration

Remove the breakpoint again and add this code below the Next statement.

For Each myElement As Decimal In myDecimalArray
    myElement = Math.PI / myElement

This is a different way to code a loop when you have a Collection that you can iterate over. In this new code, we're using the math constant Pi from a VB.NET library called Math. We replace every member of the array myDecimalArray with Pi divided by that same value. Run this code and ...

Click Here to display the illustration

... the program crashes! This is called "throwing an exception". The error helpfully suggests that you check for a "divide by zero" error. Since there's only one division, you can use the methods just explained to see if myElement is zero for some strange reason. In fact, it is! How could that be? All ten elements of the array were just initialized in the previous loop. Checking the values of that array (using a Watch window) we see ...

Click Here to display the illustration

... arrays in .NET are zero-based so there are 11 elements, not 10. If you have programmed in other languages, such as VB6, you might expect an array declared as (10) to have ten elements. Ah, that life was that simple!

The other technique you will use a lot in debugging is the concept of "single stepping" through a program. Another reason for setting a breakpoint is to halt the program in debug mode just before some problem and then execute the statements one at a time, checking carefully to be sure that everything is working the way you expected. Often, the problem will be that the program is doing something you didn't expect. The Run, Stop, and Stepping icons are circled in a previous illustration.

In a few places, I have used a Watch window to display information. The quickest way to display a variable is to right-click on the variable when the program is halted at a breakpoint. Then click either Add Watch (to continue to display the current value of the variable) or QuickWatch (which will only display the variable temporarily). One thing you get with the full version of Visual Studio is much more extensive Watch capability.

In addition to a Watch window, we have also seen that you can use an Immediate window and an Output window in debugging. On the next page, we look at those.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2010 Express - What's New With Visual Basic .NET Express

©2014 About.com. All rights reserved.