Immediate Window, "The expression cannot be evaluated...."

28,884

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:

  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.

Share:
28,884

Related videos on Youtube

Tomas
Author by

Tomas

Updated on May 03, 2020

Comments

  • Tomas
    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
      Fredrik Mörk about 13 years
      What expression are you trying to evaluate?
    • Tomas
      Tomas about 13 years
      I get on any expression, even on 2+2
  • northben
    northben about 11 years
    You must also be on a breakpoint in order to use the Immediate Window or see objects in the Locals window.
  • Nuno Agapito
    Nuno Agapito over 10 years
    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!
  • northben
    northben over 10 years
    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!
  • user
    user over 10 years
    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.
  • Csaba Toth
    Csaba Toth over 7 years
    @bflow1 What is this > operator in Immediate Window?
  • KWallace
    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. ;-)