Page 1 of 1

Debugging Express

#1 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3224
  • View blog
  • Posts: 10,825
  • Joined: 12-December 12

Posted 14 February 2013 - 12:30 PM

This tutorial will guide you through the essential debugging features of Visual Studio (VS). I am using VS Express 2012 and C# but the steps are essentially the same for VS (full) and VB.NET.

There is also a very good tutorial on DIC here which complements this one. That one is an excellent walk-through of the debugging process, and describes the thought-processes necessary to correctly debug your application. My version is more of an introduction to, and overview of, the Visual Studio features that you can use to debug. I recommend reading both!

The shortcut-keys are for the VS Express version, followed by the VS (full), or alternative, shortcut.

Here is the C# Console Application code that I will be using.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            int[] numbers = { 10, 20, 45, 50, 80 };
            int total = 0;

            for (int i = 1; i <= 5; i++) {
                total += numbers[i];
            }

            Console.WriteLine("The total is {0:D}", total);
            Console.ReadKey();
        }
    }
}

If I run this I receive the following error message:

Posted Image

If you press Break at this point you are already beginning the debugging process!

[Spoiler: The solution is for (int i = 0; i <= 4; i++) { because array-indexing starts at zero.]

When debugging you can step-through your code one line at a time, observe what happens to your variables, jump to specific lines, etc., etc. To START THE PROCESS you can:

1. Choose the Debug menu and Step Into (or press the F11 or F8 function key).

Posted Image

Continue to press F11, stepping through your code one line at a time.

WHAT YOU CAN DO WHILE STEPPING THROUGH

Point your mouse over any variable (and other values) and a pop-up will display its current value. If it is, for example, an array then you can click on the plus sign that appears within the pop-up to expand it, showing more details that you can navigate through.

Posted Image

This works for object-references, etc., as well, and you can navigate through entire structures!

As you are stepping through there are two other options on the Debug menu: Step Over (F10 or Shift-F8) and Step Out (Shift-F11 or Ctrl-Shift-F8).

Posted Image

Step Over: If you are about to call another function or method, just run through it without pausing, returning to the current code block.

Step Out: If your code has called (moved into) another function or method then complete this without pause, returning to the previous (calling) code-block.

You can also drag the yellow arrow on the left up or down to skip, or re-execute, lines;
You can right-click a line and choose Run to Cursor (Ctrl-F10).

You can abandon debugging at any point by pressing the red square (Stop Debugging) (Shift-F5 or Ctrl-Alt-Break). Or press the Start button, which now says Continue (F5) to run the rest of your code. (Generally it is better to let the code run to completion, particularly for more complex applications, so that all object-references are correctly disposed of.)

***************

2. Click to the far left of a statement to create a Breakpoint (F9) - repeat to remove the breakpoint (this is available from the Debug menu as well). Press Start and the code will run until it reaches the breakpoint and then pause for you to step through.

Posted Image

You can set a number of Breakpoints - Ctrl-Shift-F9 removes all breakpoints.

Refer back to point 1. for details of what you can do while stepping through.

3. Right-click an executable line (not a comment) and choose Run to Cursor (Ctrl-F10) - my favourite method! The code will run all the way until it reaches this line (assuming, of course, that your code will actually encounter this line).

Posted Image

Refer back to point 1. for details of what you can do while stepping through.

Locals Window
The Locals Windows will display the values of all (currently in scope) variables, including arrays, lists, etc. Once you have started debugging choose the Debug menu, Windows, Locals (Alt-4).

Posted Image

Note The Debug, Windows menu option only shows a limited number of windows until you actually start debugging - then many other windows become available to you. Investigate the Immediate, Watch and Call Stack Windows.

The Locals window appears as a separate tabbed panel (usually to the bottom-left of the screen) alongside the Output and other windows.

Posted Image

MSDN
I've linked to the VS 2010 version, rather than 2012, to avoid the distraction of any Metro features.

Is This A Good Question/Topic? 4
  • +

Replies To: Debugging Express

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Posted 14 February 2013 - 01:40 PM

Nice work Andrew. I've added it as my first link to the FAQ "How do I debug?" so it should see plenty of readers

tlhIn`toqs FAQ said:

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3224
  • View blog
  • Posts: 10,825
  • Joined: 12-December 12

Posted 14 February 2013 - 02:00 PM

Thank you @tlhIn`toq. I was hoping you would consider it a useful addition :)

Note for readers: There is a VB version of this tutorial as well here.

This post has been edited by andrewsw: 14 February 2013 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Posted 27 August 2013 - 07:32 AM

Never noticed this tutorial. I've added it near the other debugging tutorial in the Learning C# series.
Was This Post Helpful? 0
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Posted 17 September 2013 - 10:52 AM

I made it all the way through college debugging with writeLines and printf. Only since I started working have I really learned the proper way to debug.. it's so much easier this way!

This tutorial is excellent, thank you for writing it. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Posted 17 September 2013 - 10:55 AM

I didn't learn about all the things I could do with the debugger until I was at my second professional programming job. But once you understand how to use it correctly, going back and working without it is insane.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1