473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why doesn't 'using' command allow multiple objects?

I always wondered why the using command can only take one object. I
always find that this isn't sufficient for drawing so I end up always
disposing in my finally block but then I have to remember to put
everything in there.
Why can't the using command just allow something like this?

using (Pen mypen = new Pen(Color.Black), Brush mybrush = new
SolidBrush(Color.Blue))
{
}

I thought about creating some object that takes an array of
IDisposable objects in its constructor and then calls their dispose
methods from its dispose method but that would be a very clunky
interface to deal with. I would much rather just use a try...finally
pattern.
Jun 27 '08 #1
3 1911
Israel <is**********@hotmail.comwrote:
I always wondered why the using command can only take one object.
It can create multiple objects of the same type, but not of different
types.
I always find that this isn't sufficient for drawing so I end up always
disposing in my finally block but then I have to remember to put
everything in there.
Why not just nest using statements?

using (Pen mypen = new Pen(Color.Black))
{
using (Brush mybrush = new SolidBrush(Color.Blue))
{
}
}

Alternatively, you don't need the outer braces - using statements stack
nicely:

using (Pen mypen = new Pen(Color.Black))
using (Brush mybrush = new SolidBrush(Color.Blue))
{
}

You should be aware, by the way, that the using statement isn't like a
normal method - it's a language construct.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On Jun 26, 3:42*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Why not just nest using statements?
I guess it just bothered me to have multiple indentations. Mostly for
preference reasons but also because if had two disposable objects used
within an area and then later changed that to 3 and only added a
single line within that region then the file would look like I made a
whole bunch of changes to many lines (unless I ignored whitespace
changes) because it had to indent a bunch of code. At what point does
the indentation become overwelming? 5, 6 or 7 levels?
Alternatively, you don't need the outer braces - using statements stack
nicely:
I wasn't aware of that but it makes sense since that's how the 'if'
statement works. I guess that's another personal thing but I always
make it a policy to never use 'if' statements without braces because
it scares me; the conditional code isn't properly contained with its
own scope and could spill out all over the place :) Also it was a
coding standard at my first job out of college and I guess it just
stuck with me.
You should be aware, by the way, that the using statement isn't like a
normal method - it's a language construct.
I guess that's why I figured they could have easily done whatever they
wanted when they first created C# since C++ doesn't have anything like
that. Was 'using' barrowed from VB6? Or was that a different using
because they didn't have the concept of dispose before .NET.
Jun 27 '08 #3
Israel <is**********@hotmail.comwrote:
On Jun 26, 3:42*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Why not just nest using statements?
I guess it just bothered me to have multiple indentations. Mostly for
preference reasons but also because if had two disposable objects used
within an area and then later changed that to 3 and only added a
single line within that region then the file would look like I made a
whole bunch of changes to many lines (unless I ignored whitespace
changes) because it had to indent a bunch of code. At what point does
the indentation become overwelming? 5, 6 or 7 levels?
If you've got more than 4 IDisposables in scope at a time, it's worth
refactoring IMO. However, with pretty wide monitors these days line
limits aren't what they were. I try to avoid having too much
*non-whitespace* per line, but having a reasonably large indentation
doesn't bother me much.

I've also found that changing indentation to 2 spaces instead of 4
makes code a lot nicer to read.
Alternatively, you don't need the outer braces - using statements stack
nicely:
I wasn't aware of that but it makes sense since that's how the 'if'
statement works. I guess that's another personal thing but I always
make it a policy to never use 'if' statements without braces because
it scares me; the conditional code isn't properly contained with its
own scope and could spill out all over the place :) Also it was a
coding standard at my first job out of college and I guess it just
stuck with me.
I do the same - and in fact I almost always indent using statements
too, but then I don't mind the indentation.
You should be aware, by the way, that the using statement isn't like a
normal method - it's a language construct.
I guess that's why I figured they could have easily done whatever they
wanted when they first created C# since C++ doesn't have anything like
that. Was 'using' barrowed from VB6? Or was that a different using
because they didn't have the concept of dispose before .NET.
As far as I'm aware the "using" statement originated with C#. And yes,
they potentially could have made it declare more than one variable, but
I don't see it as a significant flaw that they didn't.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
0
by: Gazelle | last post by:
I have a dilemma, that I am hopping I can find some help with. He is the back story so everyone sort of understands what it is that I am trying to accomplish. My problem: I have multiple...
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
34
by: davehowey | last post by:
I have a problem. I'm writing a simulation program with a number of mechanical components represented as objects. When I create instances of objects, I need to reference (link) each object to the...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
7
by: Birky | last post by:
All, I have scrubbed the archives to see if someone else has requested this type of help but I have not been able to find anything that fits this scenario. I have a form (which is working...
2
by: DC | last post by:
Hi, I am using a GridView to present data in a DataTable, which I store only in ViewState and when the user hits the "OK" button the rows in the DataTable will be used to execute transactions. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.