Page 1 of 1

Debugging Express Rate Topic: -----

#1 andrewsw  Icon User is online

  • Provides a RESTful service
  • member icon

Reputation: 1043
  • View blog
  • Posts: 3,284
  • Joined: 12-December 12

Posted 10 February 2013 - 07:39 AM

*
POPULAR

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

There are other similar Tutorials on DIC - which you may prefer, I don't mind! I wanted to create a, hopefully, simple version, but with a little more detail. (Or, at least, an alternative version.)

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

Here is the VB.NET Console Application code that I will be using.

Module Module1

    Sub Main()
        Dim numbers() As Integer = {10, 20, 45, 50, 80}
        Dim total As Integer = 0

        For x As Integer = 1 To 5
            total += numbers(x)
        Next
        Console.WriteLine("The total is {0:D}", total)
        Console.ReadKey()
    End Sub

End Module

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 x As Integer = 0 To 4 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.

4. In VB you can type the word Stop on a line and then press Start; it will run to this line and then pause/stop and you can continue stepping-through. This is useful if you know that you will probably need to test the same section of code many times, and over several sessions. (Don't forget to delete the word Stop when you've finished!) Generally, though, you might prefer to use a Breakpoint rather than Stop.

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? 5
  • +

Replies To: Debugging Express

#2 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 138
  • Joined: 03-August 12

Posted 10 February 2013 - 10:46 PM

i love this tutorial.... :)
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 423
  • Joined: 01-February 13

Posted 01 March 2013 - 01:26 PM

If everyone read this we would not have much to do around here. haha Nice Tutorial andrewsw. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Provides a RESTful service
  • member icon

Reputation: 1043
  • View blog
  • Posts: 3,284
  • Joined: 12-December 12

Posted 01 March 2013 - 01:36 PM

View PostIronRazer, on 01 March 2013 - 01:26 PM, said:

If everyone read this we would not have much to do around here. haha Nice Tutorial andrewsw. :bigsmile:

Thank you very much. I quite agree, the more people I can persuade to read this the better :)
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 423
  • Joined: 01-February 13

Posted 16 March 2013 - 09:20 AM

Quote

Thank you very much. I quite agree, the more people I can persuade to read this the better :)

I have given this link to a few people. I just hope that they actually read it and tried it. I am no expert but, not a beginner and i have to debug some of my code sometimes. I think it should be one of the first things they teach in computer programming classes. I also think they should teach them to use the Option Strict and Option Explicit options in there code. That helps prevent some errors before they happen. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1