Visual Studio Debugging Technique - Inspect variables fast and in place

in #technology8 years ago (edited)

All programmers know how to do basic debugging. I won’t cover that in this series of techniques. I’ll talk about some relatively advanced debugging method which you may not know but will make you powerful once you learn them. I will use C++ and Visual Studio 2015 for the whole demo. 

In this article, I’ll use this piece of code for all demos. 

int main()
{
    vector<int> iVec;
    vector<string> sVec;
    int i;
    for (i = 0; i < 100; ++i)
    {
        iVec.push_back(i);
        sVec.push_back(string(i, i));
    }
    return 0;
}

 

Even if the program is not running yet, when you put the mouse on a variable, it will show the type of this variable. So you don’t have to page up to find the declaration of it.

When the program is under debug, if the mouse is moving over a variable, the value of the variable is evaluated and displayed. It is allowed to expand the variable to inspect members if it is of a complicate type. The following demonstrates how to inspect vectors. Same for class or structure variables.

The little pin in red rectangle allow you pin a variable on the screen so you can monitor it. Once it is pinned, you can move it to a blank area so it does not hide any code. This floating window lets you monitor variables during debugging without occupying spaces in Watch window, as it is not easy to find variables in different functions if there are many in the watch list. In this floating window, you can even add a comment for it to remind yourself

You can change the value of a variable by clicking on the value and change it, and it is changed immediately. 

If you are inspecting the value but you want to read the code behind the display window, you can move the mouse away to cancel the display to read the code and then display the variable again, but this is not convenient, especially when you are checking a very complicated variable, for example, an instance of a class with hundred of member variables. The trick here is to press the Ctrl key, the variable display window is pretty much transparent so you can read the code behind. When you release Ctrl key after the checking of code, the variable display comes back and you can continue inspecting the variable. 

During the inspection, if you want to add a variable or a member in a variable to the watch window, you can right click on the variable or a member of it and choose to add it to Watch

Before we have this, all of these is done in watch window, which make the window kind of messy. From Microsoft, this is supported form Visual Studio 2010. If you don’t know it yet, this is the time to master it and perform debugging like a professional, like me!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.030
BTC 65669.27
ETH 2668.58
USDT 1.00
SBD 2.87