Using an operator with multiple system resources

I used the using statement in both C # and VB. I agree with all the critics regarding nesting using operators (C # looks good, VB is not much)

Therefore, referring to me, I was interested in improving my VB applications, using the "use" of more than one system resource in one block:

Example:

 Using objBitmap As New Bitmap(100,100) Using objGraphics as Graphics = Graphics.From(objBitmap) End Using End Using 

It could be written like this:

 Using objBitmap As New Bitmap(100,100), objGraphics as Gaphics = Graphics.FromImage(objbitmap) End Using 

So my question is the best method?

My gut tells me that if resources are related / dependent, then using more than one resource in a using statement is logical.

+7
coding-style using-statement
source share
2 answers

My main language is C #, and most people prefer to "stack" when you have a lot of them in one area:

 using (X) using (Y) using (Z) { // ... } 

The problem with the only using VB.NET statement is that it seems cluttered and it will probably fall from the edge of the screen. At least from this point of view, several users look better in VB.NET.

Perhaps if you combine the second syntax with the continuation of the line, it will look better:

 Using objBitmap As New Bitmap(100,100), _ objGraphics as Graphics = Graphics.FromImage(objbitmap) ' ... End Using 

This will bring you closer to what I consider more readable.

+11
source share

They are both the same, you should choose the one that you think is the most readable, since it is identical at the IL level.

I personally like the way C # handles this by resolving this syntax:

 using (Foo foo = new Foo()) using (Bar bar = new Bar()) { // .... } 

However, I find the VB.NET equivalent of this form (your second example) to be less readable than the nested Using statements from your first example. But this is just my opinion. Choose the style that is best for the readability of the code in question, since this is the most important, given that the output is identical.

0
source share

All Articles