Using Set Next Statement to test code paths

Sometimes, it's the little things that make a big difference to my productivity.  One of those little things is the addition of Set Next Statement (a.k.a. set instruction pointer / setip) support in the .NET Compact Framework v2.

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:

  1. You cannot set the next statement outside of the current method
  2. 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 20005 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.  The next statement cannot be set to another function.

My favorite way to use Set Next Statement is in testing a new block of code.  Provided that the code does not significantly alter the runtime state of my application (ex: modify a global field which impacts the re-running of the code I wish to test), I typically do the following:

  1. Set a breakpoint just prior to the code I wish to test
  2. Step through the code with the default (compiled in values) inputs
  3. Right click to the first line of the interesting code and select Set Next Statement
  4. Use the Autos / Locals / Watch window to modify the variables containing the input values
  5. Repeat steps 2 through 4 as desired

The above steps will also allow you to experiment values that may help fix calculation related bugs (ex: centering text in a form).  I also use the above steps to provoke error conditions in my code, to make sure the error handling code behaves correctly.

Have fun!
-- DK

This posting is provided "AS IS" with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software. Any and all details are subject to change.

Comments (0)

Skip to main content