Debugging 101: How to skip over code in the Visual Studio Debugger

One of the questions that comes up regularly on newsgroups is "How do I skip over a section of my code in the Visual Studio debugger?" The answer depends on the meaning of the word "skip" but in general, there are three ways that you can skip over a section of code in the debugger. I'll describe these three techniques using the following code sample.

   1: public bool DecrementUnitsInStock(int quantity)
   2: {
   3:    bool success = Product.DecrementProductUnitsInStock(this.ID, quantity);
   4:    if (success)
   5:       this.UnitsInStock -= quantity;
   6:    return success;
   7: }

Step Over (F10)

Step Over (F10) is by far the most well known command for skipping over an entire method but still execute the function that you are stepping over. When you Step Over a line, it executes all method calls that are on that line (including methods that take other methods as their argument) and stop on the next line. However, if any of the methods that you are stepping over contain a breakpoint or throw an exception, the debugger will stop in the method.

In the screenshot below, pressing F10 will execute the DecrementProductUnitInStock() method without tracing through it and will stop at the next line.

Step Over (F10)

Run To Cursor (CTRL+F10)

Think of Run To Cursor as an "invisible one-shot breakpoint". What does that mean exactly? It means that just like a breakpoint, the debugger will execute your application up to where your cursor is and break. In the following example, I placed my cursor on the line "return success" and pressed CTRL+F10. The debugger ran all the code up to (but excluding) the line "return success" and stopped.

Run To Cursor is very useful when it comes to skipping over parts of a function that you don't want to step through. For example, if you have a loop in your method and you don't want to step through the loop, set your cursor just after the loop and select Run To Cursor (CTRL+F10).

Run To Cursor (CTRL+F10) 

Set Next Statement (CTRL+SHIFT+F10)

Set Next Statement is a powerful debugging feature for controlling application execution, granted that it's used correctly. If used incorrectly, it can lead to all sorts of weird and wonderful behavior in your application. In a nutshell, Set Next Statement allows you to jump around in a method without executing any code in between. In the code segment below, I moved the yellow arrow to the "return success" line which means that I skipped the execution of the if statement.

Set Next Statement is frequently used in conjunction with edit and continue. That is, after changing some code during the debug session, you can set the next statement to the location before the change and re-execute the code to see the affect of your changes.

Also, instead of using the shortcut CTRL+SHIFT+F10, you can drag the yellow arrow to the location in your code that you want to execute.

Set Next Statement (CTRL+SHIFT+F10)

If you use other techniques to skip over code in the debugger, feel free to leave a comment.

Habib Heydarian.

Comments (1)
  1. What about setting breakpoints with conditions?  Like "break when this value is true" which is great for catching special cases, or "break after this line is executed 50 times" whic is perfect for debugging loops!

Comments are closed.

Skip to main content