visual-studio 为什么我不能在即时窗口中声明新变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2397134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:37:59  来源:igfitidea点击:

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

.netvisual-studiodebugging

提问by Tomasi

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

这可以节省我很多时间。有时我发现自己在 watch 或直接窗口中写这样的东西:

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?

有什么办法可以做到这一点吗?在任何未来版本中是否会支持我想要的东西?

回答by huha

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:

在 VS2015 中,您可以在立即窗口中声明变量。但是你必须用分号结束你的命令:

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"

回答by AMissico

Do not use Dim

不要使用昏暗

Hyman = 12
? Hyman
12 {Integer}

回答by Brian Rasmussen

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).

使用 C#,您可以在调试期间在即时窗口中声明变量(老实说,我不知道它是否只是 VS2008 功能,但我刚刚在 VS2008 团队版中验证了它)。

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.

您不能在 Watch 窗口中声明变量(实际上错误消息说“声明语句只允许在立即窗口中使用”)但您可以查看您在立即窗口中创建的任何变量。

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

此外,您不能var在立即窗口中声明变量时使用,但除此之外,您可以执行您所要求的操作。

回答by Aaronaught

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

@AMissico 指出了我之前的错误——显然你可以在 VB.NET 中通过隐式赋值来做到这一点。

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:

如果您需要这种级别的调试信息,最好将其放入您的程序逻辑中 - 这样,当未来的维护程序员必须调试相同的代码路径时,他们将拥有所有相同的详细信息,而无需“询问”深层嵌套的对象图。而不是写MyObject.Function1().Function2().Function3(),写:

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 Visualizerif you need to get a detailed representation of a complex object or hierarchy.

如果您需要获得复杂对象或层次结构的详细表示,您也可以考虑编写自己的调试器可视化工具

回答by M.A. Hanin

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).

这可能是另一种选择:您可以使用对象测试台创建 MyObject 的实例,并使用您需要的任何参数调用该实例的方法(或者只调用该方法而不创建实例,如果它是静态方法) .

回答by Jeffrey L Whitledge

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.

除了 Aaronaught 所说的,您描述的此类构造违反了得墨忒耳定律。您可能需要考虑重构您的程序,这样就不需要这样的结构了。事实上,即使提供的解决方案也违反了适当的关注点分离。

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

如果一个方法引用不超过一个间接级别,那么调试器的行为将更适合应用程序。

回答by Ogglas

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

对@huha 答案的澄清。如果你看到error CS0726: '' is not a valid format specifier你可能只是忘记了一个.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