Sunday, November 3, 2013

Debugging in Visual Studio, Breakpoints

Breakpoints Basics

Last time we talked about the basics of running the debugger in Visual Studio.  Today let's talk about breakpoints.  There are several ways to add a breakpoint to your code.  A note about colors in this post: I will describe items in the color they appear in my IDE, it's possible you have your colors set differently than mine.  Either way, the colors mentioned in the writings will help guide you through the pictures included.

When Can I Add a Breakpoint?

  • A breakpoint can be added before you begin to run your code. 
  • A breakpoint can also be added during runtime by placing a breakpoint anywhere in the code.  If you expect to hit a breakpoint you added during runtime you should probably place it in a section you haven't yet passed in the code or at least in a section there is a way for you to return to.
  • A type of breakpoint can be added dynamically before running the debugger and without actually adding a breakpoint.  We'll call this Run To Cursor and cover this later in the post.

Where Can I Place Breakpoints?

You can add a breakpoint just about anywhere in your code.  Some places you cannot add breakpoints are listed below:
  • Whitespace.
  • Comments.
  • Decision structure with no actual code.  (i.e. if and else if are ok, the fall-through else will not be a place you can add a breakpoint)  Breakpoints can be added in a code block contained in the fall-through else.
  • Try and Finally cannot have a breakpoint added.  You can however add one to your Catch.  Breakpoints can be added in a code block contained in your Try/Finally.
  • Case and Default in a switch cannot have a breakpoint.  Breakpoints can be added in a block of code contained in your Case or Default.

Adding Your Breakpoint

  • One way to add a breakpoint is to click in the gray vertical space to the left of your code.  Your breakpoint will appear where you've clicked. 
 
  • Another way to add a breakpoint is to put your cursor where you want your breakpoint, click Debug then in the dropdown menu click Toggle Breakpoint.
 
  • If you are into keyboard shortcuts you may also add a breakpoint by putting your cursor on the line you wish to see a breakpoint and pressing the F9 key.
  • With your cursor on the line you'd like to add your breakpoint to you may also right click on your line of code, hover over Breakpoint on the pop-up menu, then click Insert Breakpoint on the pop-up menu that comes up after that. 

     
No matter which method you use to add your breakpoint, when you have an active breakpoint you will see a solid red circle in the gray vertical bar and the line of code your breakpoint is in will be highlighted in red like below:
 

Removing Your Breakpoint

Just like adding a breakpoint, you have several options to remove a breakpoint.  You can start by reversing the steps you've already been shown:
  • Click the solid red circle in the gray vertical bar to the side of your code.  Your breakpoint will go away.
  • With your cursor on the line of code for the breakpoint you want to get rid of, click Debug then in the dropdown menu click Toggle Breakpoint.
  • With your cursor on the line of code for the breakpoint you want to get rid of, press the F9 key.
  • With your cursor on the line of code for the breakpoint you want to get rid of right click in the line of code and hover over Breakpoint in the pop-up menu that displays, then click Delete Breakpoint on the menu that pops up after that.
 
There are also a couple of other ways to remove your breakpoints:
  • You can right click your breakpoint, a menu will pop-up click Delete Breakpoint.  There are other options available we will cover many of them in future posts. 
 
  • You can also click Debug then in the dropdown menu click Delete All Breakpoints.  This method will remove any other breakpoints you have added to this entire solution.
You will receive a confirmation message, click Yes if you truly wish to delete all breakpoints.
 
  • For those of you keyboard shortcut junkies, you can fire off Delete All Breakpoints by pressing Ctrl+Shift+F9 at the same time.

Run To Cursor

One last option to break on a line of code doesn't require breakpoints.  With your cursor on the line of code you wish to break on, right click and in the pop-up menu that displays click Run To Cursor.  Your code will launch.
To escape this type of debugging, you can close your browser if you are debugging a web application, close your executable if you are debugging a Windows application, or regardless of which type you are debugging you can click the Blue Stop Button on the toolbar menu.
 
 

No comments:

Post a Comment