Search code examples
vb.netvisual-studiodebuggingimmediate-window

VB.NET - How to get the instance used by a With statement in the immediate window


VB.NET has a very handy "with" statement, but it also lets you use it on an unnamed variable, like this:

With New FancyClass()
    .Level = "SuperSpiffy"
    .Style = Slimming
    .Execute()
End With

Is there a way to get at the "hidden" instance, so I can view its properties in the Immediate window? I doubt I'll get it in the watch windows, so immediate is fine.

If you try to access the instance the same way (say, when .Execute() throws an exception) from the Immediate window, you get an error:

? .Style
'With' contexts and statements are not valid in debug windows.

Is there any trick that can be used to get this, or do I have to convert the code to another style? If With functioned more like a Using statement, (e.g. "With v = New FancyClass()") this wouldn't pose a problem.

I know how With is working, what alternatives exist, what the compiler does, etc. I just want to know if this is possible.


Solution

  • As answered, the simple answer is "no".

    But isn't another way to do it: instead of declaring and then cleaning up the variable is to use the "Using".

    Using fc as new FancyClass()
      With fc    
        .Level = "SuperSpiffy"    
        .Style = Slimming    
        .Execute()
      End With
    End Using
    

    Then you can use fc in the immediate window and don't have to remember to write a

    fc=nothing 
    

    line.

    Just some more thoughts on it ;)