Immediate Window in Design Mode
Asked Answered
S

3

8

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?

Sphygmomanometer answered 18/4, 2012 at 18:21 Comment(3)
It does work in design mode but it's awkward. Read msdn.microsoft.com/en-us/library/f177hahy(v=vs.100).aspx to get an idea of the limitations. Then relax with a nice cocktail of gin and lemonade.Circularize
VB6 was an interpreted language while in the IDE. If you favor this kind of interaction then you ought to go looking for a language that uses an interpreter, not a compiler like VB.NET. Python, Ruby, most any dynamic language really.Coventry
Igby, you had the best solution. This was mostly a rant and the thought of a drink was very attractive. :-)Sphygmomanometer
K
4

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

Kizzie answered 23/2, 2016 at 8:10 Comment(0)
R
1

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. e.g. 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

Rollback answered 12/5, 2015 at 16:23 Comment(0)
O
0

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.

Ordinate answered 18/4, 2012 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.