Immediate Window, "The expression cannot be evaluated...."
Solution 1
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.
Solution 2
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.
Solution 3
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:
- Set a breakpoint in the file your application will run through;
- Await for the application execution to be stopped (by the breakpoint or an exception) or trigger it manually (e.g.: go to the URL);
- 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.
Related videos on Youtube
Tomas
Updated on May 03, 2020Comments
-
Tomas about 4 years
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.
-
Fredrik Mörk about 13 yearsWhat expression are you trying to evaluate?
-
Tomas about 13 yearsI get on any expression, even on 2+2
-
-
northben about 11 yearsYou must also be on a breakpoint in order to use the Immediate Window or see objects in the Locals window.
-
Nuno Agapito over 10 yearsThis 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!
-
northben over 10 yearsI 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!
-
user over 10 yearsJust for posterity (this is a slightly pedantic correction to the breakpoint comment above): you can also use the immediate window when exceptions are thrown.
-
Csaba Toth over 7 years@bflow1 What is this
>
operator in Immediate Window? -
KWallace over 5 years@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. ;-)