开发者

Why can't I declare new variables in the immediate window?

This could save me so much time. Sometimes I find myself writing things like this in the watch or immediate window:

MyObject.Function1.Fuction2.Fuction3.Fuction2

开发者_运维知识库Instead I could just declare several new variables and do this in a more structured way.

However doing this is not allowed.

Is there some way that i can do this? Is there going to be support for what I want in any future versions?


Just answering the question from the headline:

In VS2015 you can declare variables in the immediate window. But you have to end your command with a semicolon:

var abc = "abcdef";
Expression has been evaluated and has no value

or

var n = 7;
Expression has been evaluated and has no value

or

int o = 8;
Expression has been evaluated and has no value

To show the result, just type the name of your variable:

abc
"abcdef"

or

?abc
"abcdef"

or

?abc;
"abcdef"

or

abc;
"abcdef"


Do not use Dim

jack = 12
? jack
12 {Integer}


A clarification of @huha answer. If you see error CS0726: '' is not a valid format specifier you have probably just forgot a semicolon ;.

Example:

DateTime t = DateTime.Parse("2020-01-01 00:00:00")
error CS0726: 't' is not a valid format specifier
t
error CS0103: The name 't' does not exist in the current context
DateTime t = DateTime.Parse("2020-01-01 00:00:00");
Expression has been evaluated and has no value
t
{2020-01-01 00:00:00}
    Date: {2020-01-01 00:00:00}
    Day: 1
    DayOfWeek: Wednesday
    DayOfYear: 1
    Hour: 0
    Kind: Unspecified
    Millisecond: 0
    Minute: 0
    Month: 1
    Second: 0
    Ticks: 637134336000000000
    TimeOfDay: {00:00:00}
    Year: 2020


With C# you can declare variables in the immediate Window during debugging (I honestly don't know if it is a VS2008 feature only, but I have just verified it in VS2008 team edition).

You can't declare variable in the Watch window (in fact the error message says "Declaration statements are only allowed in the immediate window") but you may watch any variables you have created in the immediate window.

Also, you can't use var when declaring variables in the immediate window, but apart from that you can do what you're asking.


@AMissico has pointed out my earlier mistake - apparently you can do this in VB.NET with an implicit assignment.

I'd still consider this a crutch, and if I ever found myself needing to do it, I'd start asking why the program is structured in such a way that makes it necessary.

If you need this level of debug information, it is a better practice to put it in your program logic instead - that way, when future maintenance programmers have to debug the same code path, they'll have all of the same detailed information without having to "interrogate" deeply-nested object graphs. Instead of writing MyObject.Function1().Function2().Function3(), write:

var first = MyObject.Function1();
var second = first.Function2();
var third = second.Function3();

Then you can step through the actual program logic in a debugger instead of writing an entire test script in the Immediate window.

You might also consider writing your own Debugger Visualizer if you need to get a detailed representation of a complex object or hierarchy.


This might be an alternative: You can use the Object Test Bench to create an instance of MyObject, and invoke a method of the instance with whatever arguments you need (or just invoke the method without creating an instance, if it is a static method).


In addition to what Aaronaught says, Such constructs as you describe violate the Law of Demeter . You may want to consider restructuring your program so that such constructs are unnecessary. In fact, even the solution offered maintains the violation of proper separation of concerns.

If a method references no more than one level of indirection, then the behavior of the debugger will be more appropriate to the application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜