Question

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.

Was it helpful?

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

OTHER TIPS

What's wrong with defining a variable on one line and using it in a with-statement on the next? I realise it keeps the variable alive longer but is that so appalling?

Dim x = new SomethingOrOther()
With x
    .DoSomething()
End With
x = Nothing ' for the memory conscious

Two extra lines wont kill you =)

Edit: If you're just looking for a yes/no, I'd have to say: No.

I hope there really isn't a way to get at it, since the easy answer is "no", and I haven't found a way yet either. Either way, nothing said so far really has a rationale for being "no", just that no one has =) It's just one of those things you figure the vb debugger team would have put in, considering how classic "with" is =)

Anyway, I know all about usings and Idisposable, I know how to fix the code, as some would call it, but I might not always want to.

As for Using, I don't like implementing IDisposable on my classes just to gain a bit of sugar.

What we really need is a "With var = New FancyClass()", but that might just be confusing!

You're creating a variable either way - in the first case (your example) the compiler is creating an implicit variable that you aren't allowed to really get to, and the in the second case (another answer, by Oli) you'd be creating the variable explicitly.

If you create it explicitly you can use it in the immediate window, and you can explicitly destroy it when you're through with it (I'm one of the memory conscious, I guess!), instead of leaving those clean up details to the magic processes. I don't think there is any way to get at an implicit variable in the immediate window. (and I don't trust the magic processes, either. I never use multiple-dot notation or implicit variables for this reason)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top