有没有办法在 Visual Studio 中调试 VB.NET With 子句? [英] Is there a way to debug VB.NET With clause in Visual Studio?
问题描述
有时您并不真正关心变量的名称,因为它不会超出您的子程序的范围.事实上,指定名称会增加一行额外的代码.此外,现在您要处理这个名称,这可能会增加潜在的重构工作(如果您决定稍后重命名).看看下面的代码:
Sometimes you don't really care about the name of a variable, because it does not go outside of your sub's scope. In fact, specifying the name would add an extra line of code. Also now you have this name to deal with, which may add to potential refactoring effort (if you decide to rename it later). Have a look at the code below:
Dim fileInfo As New FileInfo("filename.txt")
With New FileSystemWatcher
.Path = fileInfo.DirectoryName
.Filter = fileInfo.Name
.EnableRaisingEvents = True
AddHandler .Changed, AddressOf OnChanged
End With
它是一个完全有效的 VB.NET 结构,看起来整洁干净.但是,在调试时,假设您在 With
子句中放置了一个断点,则无法选择获取该 .Path
以确保其设置正确.
It is a perfectly valid VB.NET construct, which looks neat and clean. However, when it comes to debugging, and assuming you put a breakpoint inside the With
clause, there is no option to grab that .Path
to make sure it was set properly.
我在这里遗漏了什么,还是 Visual Studio 真的不提供对 With
语句中的 .Property
语法的调试?我使用的是 2010.
Am I missing anything here or does Visual Studio really not provide debugging for .Property
syntax inside With
statements? I am using 2010.
显然,上面的代码没有太多需要调试的地方,但是可以有很多例子说明这种 nameless With
方法何时会派上用场.
Obviously, there is not much to debug in the above code, but there can be numerous examples of when such nameless With
approach would come handy.
顺便说一句,名为 With
的子句也有同样的问题,即如果我要写:
BTW, named With
clauses have the same problem, i.e. if I was to write:
Dim fsw As New FileSystemWatcher
With fsw
.Path = fileInfo.DirectoryName
.Filter = fileInfo.Name
.EnableRaisingEvents = True
AddHandler .Changed, AddressOf OnChanged
End With
我仍然无法提取 .Path
的值,必须始终以 fsw
作为前缀.
I still cannot pull the value of .Path
, having to always prefix it with fsw
.
当您将 With
子句相互嵌套时,问题会变得越来越大.
The problem grows in size as you nest With
clauses in one another.
推荐答案
带有未命名变量的 With 语句对调试器来说是一个巨大的挑战,没有解决方法.像fsw"这样的小命名辅助变量没有任何问题,它无论如何都存在,由编译器自动生成.这不是无名的,只是无法形容.与 VB$t_ref$L0)
一样,调试器不会让您输入它,因为它使用了在 VB.NET 标识符名称中无效的字符.这是有意的,它确保它永远不会与您使用的名称发生冲突.这也可以防止它出现在自动"窗口中.
The With statement with an unnamed variable is a formidable challenge for the debugger, there's no workaround for it. There's just nothing wrong with a small named helper variable like "fsw", it exists anyway, auto-generated by the compiler. Which is not nameless, just unspeakable. Like VB$t_ref$L0)
, the debugger won't let you type that in since it uses characters that are not valid in VB.NET identifier names. Which is intentional, it ensures it never collides with a name that you used. This also prevents it from showing up in the Autos window.
您已经通过命名变量找到了正确的解决方法.从那里调试很简单,只需将鼠标悬停在变量名称上,而不是字段名称上.当然,在所有其他调试器窗口中都运行良好,尤其是 Autos 窗口将活跃起来.并且不要犹豫,放弃 With 语句,转而只编写完整的语句,IntelliSense 帮助很多让这变得不那么繁琐.
You already found the proper workaround by naming the variable. Debugging from there is simple, just hover the mouse over the variable name, not the field name. And of course works well in all the other debugger windows, the Autos window in particular will spring to life. And don't hesitate to drop the With statement in favor of just writing the statement in full, IntelliSense helps a lot to make this less of a chore.
这篇关于有没有办法在 Visual Studio 中调试 VB.NET With 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!