Some debugging tips
I was pretty sick last night and only got 1 or 2 hours of sleep. I had to stay in a little to sleep it off but now I feel better. I thought I'd put out a quick post before I get off to work.
A lot of people don't know that in visual studio .Net , when debugging, the Watch window is a very powerful beast. Most people treat the data in it as read-only. It's not. You can walk up to any variable of value in the Watch window ,click on it, and write down the new value that you want for that variable. Just like that.
- Not only is the Watch window not read-only and allows you to change while debugging, it can also evaluate expressions while debugging. For example, say you have a method in a class that accepts a value of type Class1 and returns as a string some information about that class. Say you are in the middle of debugging(running is paused). Pull up the Quick-watch window(Ctrl-Alt-Q) and type in the name of that function just as you would in C# (if you're debugging a VB.Net application you need to write the expression in VB.Net). So by writing MyFunction(this) in the quick watch window, that function actually gets evaluated and you can get a lot more information about what's going on in your debugging environment.
- The same holds true for advanced breakpoints. You might not know it, but pressing Ctrl-B on a code line brings up the “breakpoint properties“ dialog. One of the properties in there allows you to enter the breakpoint only if some condition is true. That means you can write in there any expression that evaluates to true/false. Can you guess what I'm getting at? Yes. You can call functions inside your code that return bool values that will decide for you whether the breakpoint will hit. Not only that, these functions can also do some work in the process. They can log things, they can change things. This technique may get a little dangerous, but as long as you understand that you can seriously impact the execution path of you application if you're not careful, you can go ahead and do some real advanced debugging.
- Did I also mention that you can set multiple breakpoints on the same line? You can, if you have multiple expressions on a code line. If you have a complex if statement, something like :
- if(MyFirstFunction() && MySecondFunction() || MyThirdFunction()) {}
- You can actually set three different breakpoints here. To set a breakpoint only when the code executes MySecondFunction() , put your cursor inside the name of the function call and hit F9. You'll see that a breakpoint is set in the middle of the code line. If you have multiple breakpoints on a single line, clicking the left side bar to clear the break point clear all of them.