44

When I try to evaluate expression in Immediate Window at design time, I get error:

The expression cannot be evaluated while in design mode.

If I compile ASP.NET project and try to run it in debug mode I get another error:

The expression cannot be evaluated while in run mode.

Why do I get these errors? I have used Immediate Window in the past and it worked fine even in design mode.

ZygD
  • 22,092
  • 39
  • 79
  • 102
Tomas
  • 17,551
  • 43
  • 152
  • 257

4 Answers4

32

Assuming that you aren't missing the > operator in the Immediate Window, there could be problems if you are trying to evaluate an expression at design-time in a multi-project solution or even a web project.

According to MSDN:

If you are attempting to evaluate a function in a project that is not the startup project for the solution and you receive an error, try selecting the project in Solution Explorer and attempt the evaluation again.

Also:

You cannot use design time expression evaluation in project types that require starting up an execution environment, including Visual Studio Tools for Office projects, Web projects, Smart Device projects, and SQL projects.

bflow1
  • 969
  • 1
  • 10
  • 25
  • 25
    You must also be on a breakpoint in order to use the Immediate Window or see objects in the Locals window. – northben Apr 04 '13 at 17:04
  • 2
    This is not correct @northben ! Just select a Library Project in the solution explorer(as bflow1 said) and type 1+1 in the immediate window. it will be executed! – Nuno Agapito Oct 21 '13 at 15:44
  • 1
    I think the problem was that the Immediate Window would not evaluate expressions while my application was running except if it was on a breakpoint. I'm doing mostly Python development on Linux now, so I can't easily open VS to verify this. But either way, if you got it to work, great! – northben Oct 21 '13 at 15:59
  • 1
    Just for posterity (this is a slightly pedantic correction to the breakpoint comment above): you can also use the immediate window when exceptions are thrown. – user Nov 05 '13 at 16:44
  • 2
    @bflow1 What is this `>` operator in Immediate Window? – Csaba Toth Mar 11 '17 at 20:15
  • @northben - that should be the "answer". Probably 90% or more of us looking at this question had exactly that problem, solved by exactly that answer. ;-) – KWallace Nov 10 '18 at 18:41
  • Another tip that may help, if you don't want to set an arbitrary breakpoint and try to hit it (for example, starting up an API project) you can just Debug your project and click Break All (the "pause" button at top, looks like: "||") or by pressing [Ctrl]+[Alt]+[Break] (by default). You can then run your expressions as expected. – HOT theChunKk Mar 13 '23 at 18:47
6

It's worth noting that the behavior of the Immediate window varies depending on the edition of Visual Studio you are using. If I try to evaluate a simple expression like ? 2+2 in Visual Studio 2013 Express for Web, I receive the "The expression cannot be evaluated while in design mode" error message; however, in Visual Studio 2013 Professional the expression evaluates to 4 without having to be in debug mode.

pvdjay
  • 133
  • 1
  • 7
4

As northben pointed out in a comment, if you're trying to access properties in the immediate window while your application is not running, you may get:

The expression cannot be evaluated while in design mode.

Therefore:

  1. Set a breakpoint in the file your application will run through;
  2. Await for the application execution to be stopped (by the breakpoint or an exception) or trigger it manually (e.g.: go to the URL);
  3. Type in the Immediate Window the property you want to access to (e.g. GlobalConfiguration.Configuration) – now this should get you the proper results if that property exists in that context. If it does not exist, then you will get:

    The expression cannot be evaluated while in run mode.

It's as simple as making sure you are accessing the properties in the right context.

Community
  • 1
  • 1
CPHPython
  • 12,379
  • 5
  • 59
  • 71
0

In my case I received this error while using Excel Interop after enabling native debugging. Then in the debug mode I tried this:

?xlworkbook.sheets(1).name

The process hung, I did not receive any answer, and after that every other thing, e.g. ?2+2 gave me that error:

The expression cannot be evaluated while in run mode

In order to again be able to use debug features, I had to disable native debugging.

ZygD
  • 22,092
  • 39
  • 79
  • 102