Condividi tramite


Set a Complex Breakpoint Condition

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.

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. Z

  • Anonymous
    May 04, 2010
    The comment has been removed

  • Anonymous
    May 04, 2010
    Thanks Doug! :) Z

  • Anonymous
    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. Z

  • Anonymous
    February 11, 2013
    The comment has been removed

  • Anonymous
    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