tags:

views:

498

answers:

5

In this question, a user commented to never use the With block in VB. Why?

A: 

The with keyword is only sideswiped in a passing reference here in an hilarious article by the wonderful Verity Stob, but it's worth it for the vitriol: See the paragraph that starts

While we are on identifier confusion. The with keyword...

Worth reading the entire article!

Dan Blanchard
+1  A: 

It's just not helpful compared to other options.

If you really miss it you can create a one or two character alias for your object instead. The alias only takes one line to setup, rather than two for the With block (With + End With lines).

The alias also gives you a quick mouse-over reference for the type of the variable. It provides a hook for the IDE to help you jump back to the top of the block if you want (though if the block is that large you have other problems). It can be passed as an argument to functions. And you can use it to reference an index property.

So we have an alternative that gives more function with less code.

Also see this question:
http://stackoverflow.com/questions/429890/why-with-construct-is-not-included-in-c-with-construct-is-really-cool-in-vb-net

Joel Coehoorn
Agreed, if you need to jump to the top of the block, you've got issues with that block in the first place; this sounds more of a hammer-nail argument, which is another discussion entirely.
Lurker Indeed
+11  A: 

"Never" is a strong word.

I think it fine as long as you don't abuse it (like nesting)

IMHO - this is better:

With MyCommand
    .Parameters.Count = 1
    .Parameters.Item(0).ParameterName = "@baz"
    .Parameters.Item(0).Value = fuz
End With

Than:

MyCommand.Parameters.Count = 1
MyCommand.Parameters.Item(0).ParameterName = "@baz"
MyCommand.Parameters.Item(0).Value = fuz
DJ
Right on. The question for a piece of code should be "does this make the code more easily represent the solution?". Sometimes a With block might look nicer than a short alias.
MichaelGG
Hah, I agree, I usually just use it for setting parameters and didn't even know that you could nest them until yesterday.
magnifico
If I'm not all wrong you could even say "With MyCommand.Parameters". (would at least work in VB6) :-)
MicSim
+3  A: 

There is nothing wrong about the With keyword. It's true that it may reduce readibility when nested but the solution is simply don't use nested With.
There may be namespace problems in Delphi, which doesn't enforce a leading dot but that issue simply doesn't exist in VB.NET so the people that are posting rants about Delphi are losing their time in this question.
I think the real reason many people don't like the With keyword is that is not included in C* languages and many programmers automatically think that every feature not included in his/her favourite language is bad.

ggf31416
A: 

The With keyword also provides another benefit - the object(s) in the With statement only need to be "qualified" once, which can improve performance. Check out the information on MSDN here:

http://msdn.microsoft.com/en-us/library/wc500chb(VS.80).aspx

So by all means, use it.

HardCode