Windows such as the Locals and Watch windows allow us to inspect the state of the application while debugging.
When debugging, the locals window will provide details about variables defined in the local scope of your breakpoint, typically meaning the current function/method being executed.
- Open the Locals Window via the menu (Debug > Windows > Locals)
Watch and QuickWatch Window
The Locals Window is nice, but what if we want to supply an expression or variable ourselves? With the Watch Window we can do just that.
- Open the Watch Window via the menu (Debug > Windows > Watch 1)
You may have noticed that hovering over a variable while execution is paused, 0:00 shows us the value contained in the variable. 0:04 Pretty nifty? 0:07 There are other ways to see the state of the program while debugging. 0:08 The locals window is another window that's available in debugging mode. 0:11 This window provides details about variables declared 0:15 in the local scope of the current method or property. 0:18 We can use this with the call stack window. 0:22 So if I double click on the AddSong method here, and then open up the locals window 0:24 again, we'll see the three variables that are defined in the AddSong method. 0:29 We can change the values of these variables while we're in the middle 0:35 of debugging. 0:38 Let's go back up here where we have a break point set at the beginning of 0:39 the switch statement. 0:42 Now, let's restart the debugger, We'll 0:45 enter option one here, then go back to Visual Studio. 0:50 Here we see that the option variable contains the value one. 0:54 Now let's say that we actually wanted this value to be two, 0:58 we can just double click on it and change it to two right here. 1:01 Now when we step over, we go into case two instead of one. 1:04 The locals window is limited to variables that are within scope. 1:09 The watch window will allow us to see the values of variables that aren't declared 1:13 in the method, such as static variables and object fields, and properties. 1:17 This window is empty until we add something to it that we want to watch. 1:22 We can put anything in here we like, let's add our option variable to the watch 1:26 window by right clicking on the variable and clicking, add watch. 1:31 Here, we see that it appears and we can see its value. 1:36 Like the locals window, we can also double click in the value cell and 1:39 change the value while we're debugging. 1:43 We can also evaluate expressions in here. 1:47 Let's create a new instance of song in the watch window right here. 1:49 So say, new Song, 1:54 Artist="Set For the Fall". 1:57 Name="Three Nails". 2:05 I'll hide the call stack window to give us more room. 2:12 One of the things you may notice is that we now have this expander icon here. 2:17 Expanding this will show us the fields and properties that this object contains. 2:22 We can in turn add them to the watch window by right-clicking on them and 2:27 then clicking, add watch. 2:31 If we want to focus in on a single variable or expression quickly, 2:37 we can use the quick watch window. 2:41 We'll use the option variable again to demonstrate. 2:44 Just right click on the option variable and click, quick watch. 2:47 Here, we can quickly inspect a variable or 2:51 expression without having to add it to the watch window first. 2:53 The watch window is great when executing expressions and seeing the result, but 3:00 we can't do anything in there to affect the actual execution of the program. 3:04 The immediate window on the other hand, 3:08 allows us to run any code we want in the context of our running program. 3:11 From here, we can create variables, evaluate expressions, call methods and 3:15 print variable values while the debugging. 3:20 I'll demonstrate this using the immediate window, 3:23 by creating a new song object that we've not added to the application yet. 3:26 And then I'll add it to the list of songs. 3:30 So I'll say, songs.Add, and notice that we're getting Intellicence here. 3:33 New song. 3:38 And say, Artist=, 3:40 just say "A". 3:44 And Name="B". 3:48 You always need to remember to put a semi-colon here. 3:52 I'm setting the name property, so we've hit this break point. 3:56 As you can see, we still hit break points when we're using the immediate window. 4:00 Now, if I hit continue to keep running the debugger. 4:04 We can see that our new song was printed in the song list. 4:09 The immediate window can really come in handy when trying something out. 4:13 Or testing expressions and working with object instances and methods. 4:16
You need to sign up for Treehouse in order to download course files.Sign up