简体   繁体   中英

Immediate Window in Design Mode

According to Microsoft: "The Immediate window is used at design time to debug and evaluate expressions, execute statements, print variable values, and so forth."

Notice it says "at design time". I have read other statements on the web to this effect as well.

However, when anybody asks why they get the "The expression cannot be evaluated while in design mode." error, everybody always states that it only works in debug mode.

I can understand some things will not work in design mode when they reference items like textbox box values. But I really don't understand why I have to set a breakpoint, run my application, and wait for it to get to the breakpoint, just to find out what 1 + 1 is.

I admit to frustration as I'm coming from Visual Basic 6.0. There I can print 1 + 1 and get 2 while in design mode. I can also call public functions and get answers while in design mode.

All of this comes from my looking for answers to type converting. Since I couldn't find an answer on the web, I decided the quickest and easiest way was to just test some statements REAL QUICK in the immediate window to see which one worked.

Is .NET a step backwards when using the immediate window?

I understand your frustration. Fortunately the development of Roslyn (Microsoft's open source C# compiler) allowed to have a so called "Interactive Windows" in Visual Studio. You need to install Roslyn, and after some easy setup steps you can evaluate C# expressions in that window: https://github.com/dotnet/roslyn/wiki/Interactive-Window

It's very different from Immediate Window since REPL loop can be somewhat isolated, but it seems like that you can even interact with your project to some extent: Can the C# interactive window interact with my code?

This dates back even to 2011-2012, and it's default part of Visual Studio 2015 CTP1 since the end of 2015: View -> Other Windows -> C# Interactive

In short, it depends on the context of your application, that is, which solution you currently have selected in the Solution Explorer and its project type. eg Web Projects, will not evaluate immediate expressions at design time because it needs to setup that environment, however, library and console type projects will.

MSDN states:

"When establishing the context for design time expression evaluation, Visual Studio references the currently selected project in Solution Explorer. If no project is selected in Solution Explorer, Visual Studio attempts to evaluate the function against the startup project. If the function cannot be evaluated in the current context, you will receive an error message. 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."

However, if you are in a web project you will need to hit a breakpoint in order evaluate expressions in the Immediate Window.

This may be covered in other answers

To me, I can do whatever you mentioned in the immediate window in Visual Studio 2008, like finding out the sum of 1+1 or finding out the length of "sdfd". Length, etc. can be done from the immediate window. So it's not a step backward in .NET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM