Page 1 of 1

Debugging Express Rate Topic: ***** 1 Votes

#1 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3250
  • View blog
  • Posts: 10,902
  • 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? 8
  • +

Replies To: Debugging Express

#2 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • 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 Addict

Reputation: 221
  • View blog
  • Posts: 735
  • 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 offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3250
  • View blog
  • Posts: 10,902
  • 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 Addict

Reputation: 221
  • View blog
  • Posts: 735
  • 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
  • +
  • -

#6 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 18-April 13

Posted 30 May 2013 - 03:30 AM

andrewsw That was the best tutorial. I understand almost every thing except two things.

Quote

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.


Can you please explain this with an example (whenever you got time).

This post has been edited by x-logan: 30 May 2013 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3250
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Posted 30 May 2013 - 04:00 AM

Hello @x-logan. I've attempted to describe their use in the Module below.

  • If, while stepping-through, your code calls another procedure or function, then Step Out will complete this procedure (without pause), returning to the next line of the original (calling) procedure.
  • If, while stepping-through, your code is about to call another procedure or function, then Step Over will run that procedure in its entirety (without pause) returning to the next line of the current code.

As you can tell from the descriptions, they are very similar options. The only difference is whether the other/secondary procedure has already been called (entered).
Module Module1
    'Add a breakpoint to the following procedure (F9)
    'perhaps calling it from the Click event of a button.
    Sub StepThroughThis()
        'Use F11 - Step Into
        StepOut()   'will then call the next procedure..
        'choose Step Over (F10) will run StepOver in its entirety,
        StepOver()
        'returning to this procedure
    End Sub

    Sub StepOut()
        '.. you'll arrive here
        'choosing Step Out now (Shift-F11) will complete
        'this procedure without pause, returning to
        'the StepThroughThis() procedure
        MessageBox.Show("This message will be displayed")
    End Sub

    Sub StepOver()
        MessageBox.Show("This other message displayed")
    End Sub
End Module

Was This Post Helpful? 1
  • +
  • -

#8 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 18-April 13

Posted 30 May 2013 - 04:24 AM

Wow.. very clear explanation. I wish to give you 5+ but not possible :blush: .
Any Way big thanks for your time :punk: .
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3250
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Posted 30 May 2013 - 04:54 AM

You are welcome ;)

Regards, Andy.
Was This Post Helpful? 0
  • +
  • -

#10 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 3,011
  • Joined: 12-January 10

Posted 30 May 2013 - 06:09 AM

debugging blahh that is what andrewsw is for....nice work man
Was This Post Helpful? 0
  • +
  • -

#11 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Posted 08 July 2013 - 09:57 PM

This is really nice tutorial. i learned many things today
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1