How to Watch Variables in Visual Studio 2017
Codes don’t always live up to their developer’s expectations. After days or weeks of coding, you’ll need to go through a debugging process to locate code errors and potentially problematic areas.
In this article, we’ll explain how to watch variables in Visual Studio 2017. Plus, we’ll provide some additional information about the debugging process.
Watching Variables in VS
Visual Studio allows you to debug all the apps you’ve created. The debugging process means that you’re running the app with a debugger attached to it. This way, a debugger shows you what’s happening with the code while the app is running.
If you’re in a debugging session, you can watch variables through QuickWatch or Watch windows. Watch windows helps you see a few variables simultaneously, while Quick Watch can show only one. This is important since you need to check whether they contain the values you intended them to have. If you find a wrong value, look at the call stack or find the location where the value was set.
Watch Variables in Watch Window
It’s possible to follow variables in a few Watch windows simultaneously. Here’s how to watch them:
- Make a breakpoint, select “Debug” and then “Toggle Breakpoint,” or simply press F9.
- Start your debugging process after clicking the green arrow with “Start” on it and then choose the debugging option or simply press F5.
- Open Watch and select “Debug,” then “Windows” and “Watch” or “Watch 1”, or enter Ctrl + Alt+W > 1. You can also open additional windows with numeration 2, 3, and 4.
- Open the Watch window, find one empty row and select it, and enter variable “a,” “b,”, “c.”
- Continue your debugging process.
Observe a Variable with QuickWatch
To look at a variable in QuickWatch, here’s what you need to do:
- Create a breakpoint on the a= a + b line.
- Set your debugging process in motion and execute pauses at the breakpoint.
- Find the variable “a” in the code and select it.
- Select “Debug” and then “QuickWatch” and press Shift+F9.
- Once the QuickWatch dialog pops, the variable is in the Expression box.
- Evaluate an expression using the variable, try different typing expressions, and then select “Reevaluate.”
- To add an expression or a variable from QuickWatch to the Watch window, select Add Watch.
- Select a “close” option to close your QuckWatch window.
- Continue debugging and observe what’s happening with variables in the Watch window.
Debugging Process
Before trying to fix any problem, try to clarify what kind of problem you might expect in your app. If there is a bug, you always need to test your app with a debugger. Otherwise, you won’t know where the problem is and how to fix it.
Firstly, you need to know what you can expect to happen with your app. Errors are often seen as positive, as they help you manage possible problems you might face while using the app.
Through the debugging process, you can access every variable and any change you’ve made. You can then find out the incorrect values assigned and when.
Locate the Problem
In debugger mode, a debugger is actively analyzing your app while allowing you to pause the app at any time. In turn, you can go through the code line by line and inspect everything in detail. In Visual Studio, debugging starts in three ways:
- When you press F5.
- When you use “Debug,” then “Start Debugging” menu.
- Open “Debugging Toolbar” and click on the “Start Debugging” button.
Breakpoints
Visual Studio allows you to set breakpoints by placing the cursor on one of the lines and pressing F9. They’re quite helpful when it comes to reliable debugging as they indicate where VS can pause your code. This way, you can take a closer look at your variables, memory, code sequence, and other important elements.
Unit Tests
Debuggers can only be as successful as you program them to be. With unit tests, you can express your code’s intention or purpose, and only then will the debugger be capable of finding code mistakes for you. If you’re not aware of the areas that might be problematic, a debugger won’t be of great help.
Code and Debug
Software developers have been known to write code which doesn’t perform as anticipated. Debugging is a process that every developer needs to go through to find mistakes and correct them effectively.
Now that you know how to watch variables in Visual Studio 2017, you’ll enjoy working in Visual Studio even more.
Have you been using Visual Studio for writing code? What are your impressions? Let us know in the comments section below.