Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add watch without complete namespace in Visual Studio

I add code's piece to watch window from code at run-time by copying it from code and adding it to the watch window. If the code contains a method like Convert.ToString(), XMLDocument.Load(), File.Write() etc. of .NET's default namespace such as System.IO, System.Xml etc., then it gives an error: "The name 'Convert' does not exist in the current context" but it does not give an error when I add complete namespace to the added piece of code in watch window, like: System.Convert.ToString(123) gives correct value. My program's code does not have complete namespace before any .NET's framework method called in it because if I add namespace before any method's call then what is the use of adding namespace on top of each file using "using" keyword. If I add a namespace above a code in a file then I do not have to add whole namespace in that file's code before the method's call. What can I do apart from adding full namespace in watch window every time I copy a code to the watch list, so that the watch does not give error. Please see screenshot below: enter image description here

Update: I add method in the watch list to check the result of a method before it is executed. It makes sure the the method execution will not give any error or exception on execution and I can edit the code because the method has not actually been executed in the program's code execution. I put a break-point on that method's calling code and add that code in watch window first to check if there is any error, because if I do not do that then I have to re-run the whole program again to correct the value next time.

like image 381
Computer User Avatar asked Jul 04 '13 18:07

Computer User


People also ask

How do I add a watch to Visual Studio?

Select the variable a in the code. Select Debug > QuickWatch, press Shift+F9, or right-click and select QuickWatch.

How do I add a watch in Visual Basic?

Right-click on a variable choose “Add Watch” in the context menu. Right-click on a variable in the DataTip and choose “Add Watch” “Add Watch” button from QuickWatch.

How do I add a code to my Visual Studio watch?

Make sure that the code branch you're breaking on will be executed. When the breakpoint is hit, add your variable to the watch window in any of the following ways: Right click on the variable in code, and select "Add Watch"

How do I automatically add a namespace in Visual Studio?

To do it, you may either manually type the Using Namespace or you just right click on the class name and select Resolve > Namespace. But using “Ctrl+.” you can automatically add the namespace in your code.


1 Answers

The debugger uses the context where the current instruction pointer is (the little yellow arrow on the left of the source window) to try to evaluate the function.

So if the line of code where you are stopped has "using System;" at the top of the file, you should be able to type Convert.ToInt32(123) into the watch window. If you are in a different file that doesn't have that using, you'll have to full qualify the name.

I tried this with the following test case:

// Main.cs
using System;
namespace TestCon
{
    class Program
    {
        static void Main(string[] args)
        {
            Foo foo = new Foo();

            Console.WriteLine(Convert.ToString(123));
            Console.WriteLine(Convert.ToInt32("234"));
        }
    }
}

//Foo.cs (note that there are no using statements in this file)
namespace TestCon
{
    class Foo
    {
       public Foo()
       { }
    }  
}

If I step to any point in main.cs file I can copy the Convert expressions to the watch window without the System namespace qualifier and they will evaluate. If I step into (or runt to a breakpoint) in my Foo() constructor, I get the "The name 'Convert' does not exist in the current context" error unless I add the System namespace qualifier to the beginning.

Note: Even when the expression can be evaluated you end up having to hit the refresh button (the two arrows in a circle near the right of the watch window) frequently because the debugger can't tell if a call into the CLR will cause side effects.

Hope that helps.

like image 71
David W Gray Avatar answered Sep 28 '22 03:09

David W Gray