How to Setup Watches to Debug Variables in Visual Studio
February 7, 2023
• 13,553 views
visual studiovisual studio tutorialvisual studio tutorial for beginnersdebuggingdebugging C#debugging c# code in visual studiodebugging c# codedebug c#debug watchdebug watch visualwatch variable visual studiowatch variable C#how to debughow to debug visual studiovisual studio 2022visual studio 2022 tutorialdebugging for beginnershow to watch a variable in visual studiodebugging dotnetvisual studio debuggingvisual studio watch window
Using visual studio and wondering how you can make your debugging a little bit easier? Maybe you've heard of watches or watchpoints, but you're not familiar with how to use them? No sweat!
This is a beginner level video that should help you see how you can easily pull up your watches in visual studio and make your debugging easier!
If you're looking for beginner content, check out:
https://www.youtube.com/watch?v=agABzm60spg&list=PLzATctVhnsgh_G9L3jgFROZKXmVLSUG8L
Check out more Dev Leader co...
View Transcript
today we're going to be looking at setting up watches for debugging inside a visual studio so I have Visual Studio pulled up here the code that we're looking at not super important but this is actually from the social media assistant series that we've been building out and what I'm going to show today is how you can set up what's called a watch when you're debugging so that you can start looking at variables with a little bit more ease so to show you what I mean what I'm going to do is assume that we have some type of issue with our um aspect scaling math and you've been seeing the output from this Twitter screenshotter and you're getting frustrated something's up how do you go fix this how do you go debug it well something that we can go do is obviously we can go click
on the left side here to drop a breakpoint in that's not super complicated but let's go run this and then see what we can start to do with watches so I'm going to press play here I guess is technically going to go start taking screenshots of a Twitter post so let's go ahead and let that run for just a second awesome Okay so we've hit our breakpoint and where we're at in our code right now just for a little bit of context is that this is gone ahead and taken a handful of screenshots from a Twitter feed and what we're going to do here is start looking at the math for aspect scaling so again the context here not super important I'm just kind of setting us up for for how we can use watches so when you're debugging normally of course in Visual Studio
you get all the tool tips where you can hover over things so if I go ahead and step over a couple of these I can see that it calculated the max height I can see it calculated the max width um I realize in this video recording the tool tips are really really tiny but um when I hover over things and when you hover over things in your own work you can see that these things pop up that's cool but it gets a little bit kind of cumbersome if you're trying to go hover over things like Collections and even though I only have six things in this array if I wanted to go figure out like what the fourth one in was and then I want to go see the image properties like I can do that for sure and I'm doing it right now to
show you but sometimes it's a little bit finicky sometimes your cursor falls off you click and Miss whatever whatever it's just a little bit of a pain in the butt and there's an easier way to kind of watch the things that you're interested in so I'm going to go ahead and show you that we can jump up to this debug menu go to Windows and then there's this watch right so this little watch menu you can go ahead and click that um it'll pop up a little uh Visual Studio window for you that you can go dock I already have my watch pulled up so I'm going to go over to here and you can see that I've actually been using watches for some of this stuff so I'll explain this in just a moment but just to call out if you haven't used it
before there's also this thing called locals so if I go ahead and click that locals is actually very similar to watch so I think it's important to mention and locals will actually give you your local variables and you can kind of watch the state of those local variables as you're debugging so in this particular case I can demonstrate that on the locals tab I could go look at my array of tweet IDs I could go look at the third thing here um and if I wanted to actually go look at the screenshots what I was doing earlier when I was hovering with my cursor I can go expand the third item and this is really handy because it's kind of like it's pinned on your screen and you can start looking at stuff so that's really cool we can do the same thing by going
to watch so I'm going to jump over to that and now in here it says add item to watch well how do you do that right well you can just type the expression so just the code that you want to evaluate and it will add it to the watch another way that you can do that is actually by highlighting something like I have tweet screenshots right I can go rightclick that and go add watch boom it's added right there I could go type that myself tweet screenshots boom same thing right so it's just two different ways to do the exact same thing and and you'll notice that as I just showed you on the local screen it's going to look very similar here it's the same idea I can kind of pin the thing that I want to watch now the things that you're watching
just like on the locals uh tab they're only with respect to the context that you're currently in so for example I can't go watch some variable in some other function if I'm not there so if I don't have access to it from the current scope of execution it won't evaluate um so that's important to note but this seems maybe kind of basic like why would I want this if I could just jump over to the locals tab anyway and get the same thing well there's a little bit of extra power on the watch tab uh and in my opinion you can start to use this for maybe doing some actual Expressions that you want to see get evaluated so as I mentioned the locals tab only has a set of fixed things that are local to your function and while that's handy if I'm and
I am really bad at trying to wrap my head around uh anything that's sort of geometric when it comes to programming I have no idea why it just never clicks for me I could go ahead and use a watch and actually evaluate some of this stuff so for example let me get rid of these other watches if I wanted to go figure out um and this maybe like this line of codee's kind of Handy right I want to go figure out which aspect ratio I'm closer to am I closer to 1: one or four to five well what I could do is I could actually highlight just this and add a watch for it right so I could see what does this evaluate to well that comes out to one what does this evaluate to I could add a watch for it that comes out
to 08 I can literally highlight this whole section at a watch for it and I can see that that's going to evaluate to false I think you can do the same thing maybe not on this tary operator I'm trying to see if I highlight over if it will show up and it doesn't I think depending on the expression you have like Visual Studio can generally like I think if I hover over yeah if I hover over four divided by five it will do the evaluation but you can do this kind of thing and then let's see if there's another good example like if I wanted to figure out I don't know I want to go make an actual rectangle because that's what we're going to do a little bit lower some of this code looks a little gnarly so I apologize but um let's even
jump into like the first Loop that we have here right if I want to figure out what my vertical offset comes out to um perhaps I'm really interested in just knowing the first part of this calculation I can go ahead and add a watch for it now I'm going to go ahead and delete the other ones I've added just to kind of illustrate that what's really neat is I'm in a loop right I've added a watch here it's a valuated to 67 cuz I cared about that part maybe I want to evaluate the whole thing let me go ahead and add a watch for that so you can see that the first evaluation is 67 the next one is 33.5 well that's cool cuz what I'm doing here is stepping through my screenshots and then doing some calculations well if I press F5 and there's
multiple Tweets in this list of tweet screenshots and there there is I can actually press F5 and it will update the evaluation based on the context so I'm going to press F5 and you can see that the watch updated to show me that the things I care about have updated it's a little bit of a contrive situation because maybe these aren't really super handy things to go watch but if you think about uh maybe some web requests that you're running or more comp Lex data structures especially when you're iterating things over and over either in a loop or because you're manually rerunning your program to try and fix stuff in my opinion setting up watches can be a really handy timesaver so just a quick one for today hopefully you found that helpful it's just a quick tip for maybe setting up how you debug
to get a little bit more context for the scenario that you're stepping through so we looked at uh the locals tab we looked at setting up watches and really the fundamental difference in my opinion is just that you can set up a little bit more specific expressions in your watches so thanks for watching give the video a thumbs up leave a comment below if you have any thoughts or questions and subscribe for more we'll see you next time
Frequently Asked Questions
What is the purpose of using watches in Visual Studio during debugging?
Watches allow me to monitor specific variables or expressions while debugging, making it easier to track changes and evaluate conditions without having to hover over each variable individually.
How do I add a variable to the watch list in Visual Studio?
I can add a variable to the watch list by either typing the variable name directly into the watch window or by right-clicking the variable in the code and selecting 'Add Watch'.
What is the difference between the locals tab and the watch tab in Visual Studio?
These FAQs were generated by AI from the video transcript.The locals tab shows me all local variables in the current scope, while the watch tab allows me to evaluate specific expressions or variables, giving me more flexibility in what I want to monitor.
