Conditional breakpoint in Visual Studio
Asked Answered
B

7

19

I want to set a breakpoint on a certain line in C# code when some other variable is equal to a specific value, say:

MyStringVariable == "LKOH"

How can I do that?

I tried to right click on breakpoint icon -> Condition and then typed MyStringVariable == "LKOH" and Visual Studio said it cannot evaluate it.

Blockus answered 19/2, 2010 at 14:1 Comment(5)
I would normally modify code and inject code like if(MyStringVariable = "LKOH") { int a=1; // dummy code to set breakpoint }Blockus
EDIT - change = to ==. THis was just a typo. Of course I used == in debuggerBlockus
Oh another clue. Actually I am using a property. My true expression is Exchange.Name == "LKOH", where Name is property. May be debugger cannot work with properties?Blockus
Expiremented. Conditions works for local variables, do not work for class properties. Am I wrong?Blockus
Yes, you're wrong. Conditions work for class instance properties. See my answer.Parsonage
D
25

Sample code:

static void Main(string[] args) {
  string myvar;
  for (int ix = 0; ix < 10; ++ix) {
    if (ix == 5) myvar = "bar"; else myvar = "foo";
  }    // <=== Set breakpoint here
}

Condition: myvar == "bar"

Works well.

Doorkeeper answered 19/2, 2010 at 14:9 Comment(0)
T
37

if (MyStringVariable == "LKOH") Debugger.Break();

you'll need System.Diagnostics namespace

http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx

Twinkling answered 19/2, 2010 at 14:14 Comment(0)
D
25

Sample code:

static void Main(string[] args) {
  string myvar;
  for (int ix = 0; ix < 10; ++ix) {
    if (ix == 5) myvar = "bar"; else myvar = "foo";
  }    // <=== Set breakpoint here
}

Condition: myvar == "bar"

Works well.

Doorkeeper answered 19/2, 2010 at 14:9 Comment(0)
M
7

Just like in code, you need to use:

MyStringVariable == "LKOH"

The double-equals is the key. Without it, it's saying it can't evaluate because your expression doesn't evaluate to a boolean.

Moseley answered 19/2, 2010 at 14:3 Comment(0)
P
5

You should be able to make this work. Are you using the Exchange instance name in the condition? The condition should be something like myExchange.Name == "LKOH" not Exchange.Name == "LKOH".

By the way, using the assignment operator = instead of the equality operator == will work but it will set the property and waste 1/2 hour of your time figuring out what the hell is going on. I made this mistake just yesterday.

Parsonage answered 19/2, 2010 at 14:32 Comment(0)
M
4

In my case, I forgot that I was debugging a VB application.

In VB equality is = not == like many other languages, thus my conditional breakpoint needed to be myString = "someValue" not myString == "someValue"

Mcmaster answered 3/8, 2016 at 16:6 Comment(0)
F
1

The variable you are testing for needs to be in scope at the breakpoint.

var x = "xxx";
{ 
  var y = "yyy";
}

brak(); // x is in scope, y isn't
Firebug answered 19/2, 2010 at 14:8 Comment(0)
S
0

For me this made it hit the conditional breakpoint.

Conditional breakpoint

Sclar answered 3/11, 2020 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.