Immediate Window, "The expression cannot be evaluated...."
Asked Answered
W

4

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.

Writhe answered 3/5, 2011 at 9:5 Comment(2)
What expression are you trying to evaluate?Anzovin
I get on any expression, even on 2+2Writhe
R
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.

Rexford answered 27/10, 2011 at 21:16 Comment(7)
You must also be on a breakpoint in order to use the Immediate Window or see objects in the Locals window.Everrs
This is not correct @Everrs ! Just select a Library Project in the solution explorer(as bflow1 said) and type 1+1 in the immediate window. it will be executed!Appolonia
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!Everrs
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.Selfjustifying
@Rexford What is this > operator in Immediate Window?Pyo
@Everrs - that should be the "answer". Probably 90% or more of us looking at this question had exactly that problem, solved by exactly that answer. ;-)Kirsch
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.Osber
F
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.

Franci answered 16/10, 2014 at 19:27 Comment(0)
A
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.

Abort answered 2/5, 2016 at 11:48 Comment(0)
A
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.

Agogue answered 2/7, 2015 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.