Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is With...End With a good practice in VB.NET? 2

Status
Not open for further replies.

steve728

Programmer
Joined
Mar 16, 2003
Messages
536
Location
US
I'm used to using With...End with while programming with MS Access / VBA. At my new job I'm learning VB.NET / SQL Server. I notice they very rarely use With...End With. I was taught to use it for optimization reasons. Is it still a good idea to use it when programming with VB.NET?

Steve728
 
I use it all the time. Plus it helps to keep your code shorter and cleaner. Don't know about the performance though.

You'll really like initialization on one line too
Dim myVar As String = "SomeValue"

Good Luck!
 
I believe the WITH construct was introduced in PASCAL.

In the early days there definitely was a massive performance boost using it because it allowed the compiler to write assembly code that preloaded CPU Index registers so that the fields could be reference by OFFSETs only.

Today, as far as I am aware, it doesn't make any dfference - especially in .NET. C# doesn't have the WITH construct and the code produced after compiling a VB.NET program doesn't contain it either. Its only benefit is readability.


Hope this helps.

[vampire][bat]
 
Actually, I can't speak for vb.net, but can for vb6. There's a definite performance boost in the latter. I suspect there is in fact a boost in .net as well, although I wouldn't be able to say anything definitive one way or the other.

The performance boost in VB6 derives from the fact that the object reference only has to be parsed once for each with statement, rather than once for each property assignment and method call in that with statement as would be the case if the object variable were referenced every time. I don't know if IL resolves multiple sequential calls to the same object reference or not; if it does, then perhaps the with statement doesn't confer a performance benefit. Otherwise, one would think that it would.

Earthandfire, perhaps you can shed light on the subject.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top