Set a Complex Breakpoint Condition

Tips Search

Set a Complex Breakpoint Condition

  • Comments 8

Menu:  [Right-Click the Breakpoint] -> Condition
Versions:  2008,2010
Published:  5/4/2010
Code:  vstipDebug0022

 

image

 

So, it turns out that the REAL power of the Breakpoint Condition dialog is the ability to execute any line of code from it.  You could do something really evil like reset a variable so you have an infinite loop:

image

 

 

Or you can use it to call external methods.  This next example illustrates this but, bear in mind, it's an extremely contrived one just to show how this stuff works.  Here is the code we want to execute:

image

 

Notice there is a variable, "x", that is given a value.  In this case, I hard-coded a value but the value would presumably come as the result of some method execution.  I want the breakpoint to only stop if the value of "x" is 20.  I have a method that returns a Boolean value in order to test for the condition I want:

image

 

I won't get into the obvious debate about whether this is a good idea or not and how the method should be constructed or if the average rainfall in the Amazon Basin is a factor here.  I'll just call the method from the Breakpoint Condition dialog.  Notice I use CTRL + SPACE to get IntelliSense here if needed and leverage the new Pascal Case feature by typing "CV" to get "CheckForValue":

image

 

Make sure the "Is true" option is selected:

image

 

And, if I put my mouse over the glyph, I can see the condition in the Tooltip:

image

 

Now I have a conditional Breakpoint that uses an external method to determine if the code should stop or not.  Naturally, you can add more Boolean logic here as well and include And / Or / Not / Xor:

image

 

Now the really cool part is you can have your checking code only be around for your Debug builds:

image

 

By surrounding your code with Conditional Compilation Directives you can easily have checking code that is only around for debugging.  Here is what the checking method would look like:

image

 

Play with this feature some and you will see why most people (including me) think that this is the most powerful Breakpoint feature there is.

  • Wow, that's pretty awesome.  Is this only for specific languages, or does it work for all of them?

  • It works for C# and VB.  Haven't tried it on any others.

    Z

  • Many developers, including myself, forget how useful Complex Break Points are. It's a powerful tool--and having IntelliSense available makes it even better. Thanks for the tip and the reminder. I've sent the link to the Tips and Tricks to all my team members.

    This is a great series Zain.

  • Thanks Doug! :)

    Z

  • The Conditional attribute is a pretty cool alternative to the  #if/#endif block.  I'm guessing this would work, too (?)...

    [Conditional("DEBUG")]

    public bool CheckForValue() {

     if (x == 20)

     {

       return true;

     }

     else

     {

       return false;

     }

    }

  • @Scott -- That code should not work as the Conditional attribute doesn't allow return values from the method.

    Z

  • Is there a possibility to say:

      " If this variable = value" break;" without given a specific line?

    So whenever the variable gets a strange value, please stop?

    Due to reflection it seems just putting a breakpoint to the usual suspects does not seem to be enough, i search a hell lot for which method does set a vairable to a value which is not alowed and does not seem to occur in normal program flow....

  • Hi Offler :)

    You can put in any code you want as a condition, even have the condition pass information to a special method just for debugging as I show above. You might also try using the "has changed" option so that whever the variable changes you can see the change happening.

    Z

Page 1 of 1 (8 items)
Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post