Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

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

Reply
 
Old 06-26-2008   #1 (permalink)
Israel


 
 

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

Israel <israeldiperi@xxxxxx> wrote:
Quote:

> On Jun 26, 3:42*pm, Jon Skeet [C# MVP] <sk...@xxxxxx> wrote:
Quote:

> > 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.
Quote:
Quote:

> > 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.
Quote:
Quote:

> > 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

My System SpecsSystem Spec
Old 06-26-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

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

Israel <israeldiperi@xxxxxx> wrote:
Quote:

> 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.
Quote:

> 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 06-26-2008   #3 (permalink)
Israel


 
 

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

On Jun 26, 3:42*pm, Jon Skeet [C# MVP] <sk...@xxxxxx> wrote:
Quote:

> 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?
Quote:

> 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.
Quote:

> 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.


My System SpecsSystem Spec
Old 06-26-2008   #4 (permalink)
Jon Skeet [C# MVP]


 
 

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

Israel <israeldiperi@xxxxxx> wrote:
Quote:

> On Jun 26, 3:42*pm, Jon Skeet [C# MVP] <sk...@xxxxxx> wrote:
Quote:

> > 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.
Quote:
Quote:

> > 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.
Quote:
Quote:

> > 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Selecting one from multiple similar objects PowerShell
get multiple wmi objects one query PowerShell
ADO.Net / Multiple SQL Statements in One Command .NET General
Modifying multiple AD user objects PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46