views:

247

answers:

4

Env: Visual Studio 2008 - C#

I have a for which runs for 1000+ times over a string array.

I want to have my app break when one of the strings matches a certain term so I can walk through at that point in my code.

Now I know I can add a piece of code that looks for this and a break point when it hits, but is there not a way to do this in the debugger?

+5  A: 

Yes, you can in the debugger. It's called a "conditional breakpoint." Basically, right click on the red breakpoint and go to the "condition" option.

A quick google turned this and this up:

P.S. The last one is VS 2005, but it's the same in 2008.

joshua.ewer
Google (bing?) are good, but it's also nice to have StackOverflow.com with all these little details too.
Coding Monkey
+3  A: 

Go to your code

  1. create a breakpoint
  2. right click on the red dot on the left
  3. select condition
  4. put something like i == 1000

or

at the middle of your loop

write

if (i == 1000){
  int a = 1;
}

and break over int a = 1;

The second method looks more like garbage, but I find it easier and faster to do

Eric
A: 

In visual studio you can set a conditional breakpoint - set a breakpoint at the point where you want to break as normal and then right click on the brown circle in the left margin and select "conditional breakpoint..." or whatever. You then type in an expression that evaluates to true when you want to break (e.g. i == 1000, or MyString = "hello world")

Kragen
+3  A: 

I'd like to add to this conversation a general debugging rant...(not directly aimed at you coding monkey)

Debugging is NOT a spectator sport. Every developer needs to know what their debugging tools do and how to get the best out of them.

With a modern language like C# and powerful IDE's like Visual Studio and even SharpDevelop there really is no excuse for (extended) debug sessions that consist solely of console.writeline() statements.

Watch windows, conditional breakpoints (and data breakpoints in native code), thread windows, the call stack, the command window, the immediate window, etc. All these things give you everything you need to solve your problems in a prescribed scientific manner rather than just floundering around with logging statements.

I challenge all developers to spend 15 mins every single day to learn a new debugging technique with the tools at your disposal.

Tim Jarvis
Right, so that's what I've been doing. Before my debugging went as far as console.writeline, hehe, busted. But now I'm looking at the visual debugger and all the features.
Coding Monkey
Excellent. Time invested in learning how to make your de-bugging tools sing, will be paid back to you 100X or more.
Tim Jarvis