How do I reference a namespace to be used in immediate or quickwatch?

ccalboni picture ccalboni · Mar 21, 2012 · Viewed 7.1k times · Source

Sometimes when I quickwatch an expression at runtime, the Quick Watch window shows an error saying the name does not exists in the current context. The same goes for the immediate window. The expression I try to evaluate, however, is perfectly recognized by the class, without throwing any compilation error.

For example, I can have the following line of code:

double x = Math.Pow(2,3);

If I stop the cursor on this line and quickwatch the "Math.Pow(2,3)" part, it gives me an error and I need to place a "System." before my expression; as I said, the same expression runs smoothly in the code window, so I'm not sure which is the "context" the error refers to.

Could it be that these debug windows reference namespaces declared in the class but can't do the same for namespace imported by the project?

Answer

BjarkeCK picture BjarkeCK · Jan 2, 2014

Solution:

Go to your program.cs file and add the usings you want your immediate window to use, this works for both Console and Windows Forms applications

Refrence namespaces to Immediate Window in a Class Library Project

If you are using the Immediate Window in "Design Time" mode, and want reference some namespaces to it, you need to set the ouput mode to Windows Application, and create a program that does nothing.

  1. Right click your project in solution explorer and click properties
  2. Under Application, change the Output type: from Class-Library to Windows Application
  3. Add the following dummy-class to your project:

Program.cs

using System;
//Add all the refrences you need immediate window to use here

namespace YourNamespace
{
    static class Program
    {
        static void Main()
        {

        }
    }
}