Set a Complex Breakpoint Condition
Menu: [Right-Click the Breakpoint] -> Condition
Versions: 2008,2010
Published: 5/4/2010
Code: vstipDebug0022
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:
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:
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:
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":
Make sure the "Is true" option is selected:
And, if I put my mouse over the glyph, I can see the condition in the Tooltip:
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:
Now the really cool part is you can have your checking code only be around for your Debug builds:
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:
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.
Comments
Anonymous
May 03, 2010
Wow, that's pretty awesome. Is this only for specific languages, or does it work for all of them?Anonymous
May 04, 2010
It works for C# and VB. Haven't tried it on any others. ZAnonymous
May 04, 2010
The comment has been removedAnonymous
May 04, 2010
Thanks Doug! :) ZAnonymous
May 04, 2010
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; } }Anonymous
July 27, 2010
@Scott -- That code should not work as the Conditional attribute doesn't allow return values from the method. ZAnonymous
February 11, 2013
The comment has been removedAnonymous
February 11, 2013
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