Sometimes you donโt care about the name of a variable, because it does not go beyond your subclause. In fact, specifying the name will add an extra line of code. You also now have this name that you may encounter, which may add to potential refactoring efforts (if you decide to rename it later). Take 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
This is the perfect VB.NET design that looks neat and clean. However, when it comes to debugging and it is assumed that you have set a breakpoint inside the With clause, there is no way to capture this .Path to make sure it is set correctly.
Am I missing something here or is Visual Studio really not providing debugging for .Property syntax inside With statements? I am using 2010.
Obviously there is not much debugging in this code, but there can be many examples of when such an unnamed With approach comes in handy.
BTW called With sentences have the same problem, i.e. if I had 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 out the .Path value, always putting it in the fsw prefix.
The problem grows in size when you insert With sentences into each other.
source share