views:

324

answers:

4

VB.Net has a the 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 it's properties in the Immeidate 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, eg. "With v = New FancyClass()", this wouldn't pose a problem.

EDIT: I know how with's work, what alternatives exist, what the compiler does, etc. I just want to know if this is possible.

+1  A: 

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.

Oli
Thanks, but I actually need an answer to the question. I am aware of how to declare variables, and exactly how a with statement functions from a compiler standpoint.
Andrew Backer
Edited. No, there isn't a syntax to grab "." on its own.
Oli
A: 

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)

Jeremy
Thanks, but I actually need an answer to the question. I am aware of how to declare variables, and exactly how a with statement functions from a compiler standpoint. Aslo, do you actually put var=nothing at the end of every function you declare, for all the locals? Thought not.
Andrew Backer
+1  A: 

as answered the simple answer is "no"

but isnt 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 dont have to remember to write a

fc=nothing

line.

Just some more thoughts on it ;)

MikeScott8
A: 

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!

Andrew Backer