18

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Captain Comic
  • 15,744
  • 43
  • 110
  • 148

7 Answers7

37

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

you'll need System.Diagnostics namespace

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

Danny G
  • 3,660
  • 4
  • 38
  • 50
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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
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.

David Boike
  • 18,545
  • 7
  • 59
  • 94
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.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
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"

Nick
  • 882
  • 2
  • 9
  • 31
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
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
0

For me this made it hit the conditional breakpoint.

Conditional breakpoint

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kitesaint1309
  • 59
  • 2
  • 8