Did you know… You can use the immediate window as a glorified calculator or side-debugger within your debugger – #241

This was tip #20 in my TechEd VS 2008 IDE tips and tricks talk.

When you want to do more than just edit the value of a variable, you can rewrite entire functions or create new ones in the immeidate window.  I found this especially useful when I was doing the math to automate dragging a tool window from a docked location to  a docking target.  I basically had to do the math to calculate a straight line between the two points in order to send the coordinates to the mouse drag functions.  If i did the math wrong, i could pull up the immediate window and play with the calculations over and over again without interfering with the main debugger, meaning that the variables and state of the main debugger would remain the same unless i purposefully modified a value in the immediate window.

A very simple example:  let’s say that you have the basic “hello world” console app, but want to print out the result of some calculation.  Notice how the console app just has “hello world”

Hello World on Output Window

If i put a breakpoint at the very end of this simple console app, i can bring up the immediate window via Debug – Windows – Immediate and do whatever i need.  Let’s say i needed to use the immediate window as a glorified calculator, i can figure out the value of 1+1, as shown below in VB

immediate window calculating 1+1

Since we’re in a console application, you can even have the value of ‘i’ printed to the console window via the immediate window.  This is where you actually want to change the state of your debugger.

Writing 'i' to console window via immediate window

and now the value of 2 appears in the console window.

value of 'i' in console window

Technorati Tags: VS2005Tip,VS2008Tip

Comments (7)

  1. David N says:

    Interesting. I wonder: Can you use this to call a method? or manually fire any event?  Hmmm…..

  2. Massimiliano says:

    There’s a simpler way, you don’t need the "i = …"

    In the immediate windows you can simply write


    naturally you can also do more complex math… :-)

    "?(1+3)*125 + Math.Sqrt(124.5d)"

  3. Vince G says:

    In VB6 you could save multi-line text to the Immediate window and hence use it as a scratch-pad to cut and paste blocks of code. Is there a similar feature in VS?

  4. Coding4Fun says:

    Sara Ford over at blogs.msdn.com has an amazing stream of tips and tricks with Visual Studio . 

  5. Сара Форд (Sara Ford) в своем блоге дает массу интересных советов по использованию Visual Studio . Текущий

  6. When my jQuery code doesn’t work, it usually means I’ve done something terribly wrong with my selectors.