Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 335
ccalboni Avatar asked Mar 21 '12 09:03

ccalboni


People also ask

How do I use Immediate Windows vs?

To display the Immediate window, open a project for editing, and then choose Debug > Windows > Immediate or press Ctrl+Alt+I. You can also enter Debug. Immediate in the Command window. The Immediate window supports IntelliSense.

How is the immediate window used in VB?

To execute code in the Immediate window Use the Immediate window to: Test problematic or newly written code. Query or change the value of a variable while running an application. While execution is halted, assign the variable a new value as you would in code.


2 Answers

In my understanding the QuickWatch and the Immediate windows are executing the code / expressions under the current executing context, so once there is a using System; in place in the code, the quickwatch window will not give any issue. You may have encountered some visual studio bug. Try restarting VisualStudio and check again.

I have not encountered this kind of issue. This kind of issue will crop up when you try to Quick Watch Math.Pow(2d, 4d); when having the code like this System.Math.Pow(2d, 4d);

like image 167
Saravanan Avatar answered Sep 24 '22 19:09

Saravanan


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()         {          }     } } 
like image 36
BjarkeCK Avatar answered Sep 22 '22 19:09

BjarkeCK