The Art of Debugging – A Developer’s Best Friend – Lesson 9 – The Set Next Statement – Repost

Download the sample project – debugging.zip

If you are not familiar with the Set Next Statement feature, it allows you to specify from which line of your application's code you wish to resume running.

Please be aware that setting the next statement can be dangerous.

It is easy to skip over critical code (ex: object creation, variable initialization, etc) and lead to unexpected application behavior and/or crash your application.

There are some limitations on which lines you can specify as the next statement: You cannot set the next statement outside of the current method You cannot set the next statement into a catch or finally block If you cannot set the next statement to your desired line, Visual Studio 2008 does an excellent job in letting you know why.

For example, attempting to set the next statement outside of the current method will cause Visual Studio 2005 to display the following message.

Unable to set the next statement to this location.

In the next sample, we will force in a statement to evaluate to true. We can do this in one of two ways. The first way is to simply drag the yellow arrow. The second way is to right mouse click on the line of code that you wish to execute.

clip_image002

As stated previously, you can also set the next statement with a simple right mouse click. This approach makes more sense if the function is quite large. Dragging the arrow on a very large function may be impossible.

clip_image004

Underneath the hood it is actually quite simple what the set next statement is really going.

It simply changes the instruction pointer.

If you know anything about assembler this is quite trivial.

But you should exercise caution

You may inadvertently skip over a key object initialization. This may cause a null reference exception later in your code.

Can you jump to other functions?

The answer is no. The reason is that the runtime may have not jitted the code for that other function. Therefore, changing the instruction pointer is impossible.

Can you go backwards?

Yes you can. This might make sense in a scenario where you wish to initialize an object to manually by moving forward and backwards as needed.

This was a simple blog to help you debug more effectively with the set next statement command.