Is there a way to debug VB.NET With clause in Visual Studio?

vb.net, visual-studio-2010, visual-studio-debugging, with-statement

Solution

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.

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.

Problem

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 ``` 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. Am I missing anything here or does Visual Studio really not provide debugging for `.Property` syntax inside `With` statements? I am using 2010. 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. 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 ``` I still cannot pull the value of `.Path`, having to always prefix it with `fsw`. The problem grows in size as you nest `With` clauses in one another.

Original source