Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Debugging is a critical skill to have as a developer. In this movie, I will show you a few debugging tools inside Visual Studio. I've opened a project called DebuggingSilverlight. I'll double-click on MainPage.xaml. This page has a simple UI with three buttons for testing different debugging scenarios. I'll start by looking at this first button. I'll double-click on the button and look at this code here. I'm using the Debug class and the WriteLine method. What this will do by default is output the string to the output window.
I've decided to bracket the call to this method in a Debug.WriteLine where I say this is before the call, and this is after the call. To test this, I need to create a Debug build and I need to attach a debugger. That's easy to do in Visual Studio by choosing Debug>Start Debugging. Before I do this, I'd like to say that it's best if you use Internet Explorer while debugging. Now, I'll click on my Debug button, and if you look here in Visual Studio, you can see a Before Call and an After Call in my output window.
I'll close the browser and return back to Visual Studio. The Debug class also has the assert method. I can use it like this Debug.Assert. It says here that it Checks for condition and outputs the call stack if the condition is false. So, what I want to do is check whether this int value is a positive number. So, I'll say Debug.Assert(tourId>0) and it'll test for a positive number. And you'll see when I'm calling it up here in line 30, when I clicking the button, I am passing a -12.
Let's test this scenario. Press F5 to run the application and attach a debugger. Click on the button again, and you see I get this Assertion Failed dialog. It tells me that something is wrong and it tells me where the assertion failed. This gives me the call stack. I'll close the browser and return back to Visual Studio. Visual Studio, like most modern IDE, supports adding breakpoints to your code windows. A breakpoint is a signal to the debugger to stop the execution of your application and break into the code editor.
Note, you cannot edit code while in break mode; you need to stop the application, edit the code, and restart. I'm going to put two breakpoints inside this method called BreakpoinButton_Click. There're a couple of ways to add breakpoints. For one, you can move to a line of code and press F9. That toggles on or off the breakpoint. If I press F9 the second time, it toggles off the breakpoint. Another handy way of turning on and off breakpoints is to select a line of code and then go over here to the margin or that gutter and click over here.
Again, clicking on it a second time turns off the breakpoint. I have two breakpoints in here. I can put different conditions on these breakpoints. I can use hit count counters on the breakpoints. That's done by right-clicking on this red dot and choosing things like Condition, Hit Count, Filter and so on. I'm good to go. I'll choose Debug>Start Debugging, I'll click on the Add Breakpoint button, and you see I now have a yellow line on my first breakpoint. That signifies that I'm just about ready to run this line of code.
There's also a yellow arrow on the left border. I can step through my code by clicking on these buttons, Step Into, Step Over, and Step Out or by pressing their affiliated keystrokes like F10 or F11. Let's do that. I'll press F11, I'll move through a couple of lines of code, and then at this point, I want to rewind and replace some of that code. I want to retry the code is a better way of saying that. So, I can take this yellow arrow and drag it up to the statement. Now, I can try that again by pressing F11.
You can also hover over variables like casePrice or volumeDiscount to see the values. If I'd rather have a more permanent view of these instead of having to hover my mouse over those, I can use the watch window. So, let me show you that demo. Stop the application. I'll put a breakpoint in this method down here WatchButton_Click. I'll put a breakpoint right here, F9, and then I'll press F5 to start the application and attach a debugger and then I'll click on the WatchWindow button. Again, I've hit my breakpoint.
Visual Studio has a number of windows you can stack on the bottom of your screen, Autos, Locals, and Watch. I'm going to move over to my Watch window and then I'm going to type in the word message down here and then I'm going to press F11 to step through one line of code. As you can see, down here in my Watch window I can see the stored value that's inside that string, Cannot complete your print job, and I can do that with any of these variables. For instance, I can put the p variable here where I can say p.Name, and then as I step through my code, I can keep an eye in that Watch window and see their values.
Visual Studio is packed with debugging tools, many more than I can discuss in this course. To see more about the rich debugging tools available, I suggest you watch the Visual Studio 2010 Essential Training course. It has an entire chapter on the Visual Studio Debugging Tools. That course covers all the debugging windows, details about breakpoints, that fantastic IntelliTrace feature and more.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98223 Viewers
61 Video lessons · 85535 Viewers
71 Video lessons · 69488 Viewers
56 Video lessons · 101826 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.