Yes but it doesn't protect the case when an exception is thrown in
cmd.Dispose().  In that case cn.Close() will not be called as Kirk said.

Jim

> -----Original Message-----
> From: Peter Stephens [mailto:[EMAIL PROTECTED]]
> Sent: Friday, May 03, 2002 5:13 PM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] try...finally vs. nested using statements 
> WAS: From C#
> to VB.NET
> 
> 
> My sample below which uses try...finally does handle the case 
> properly if
> there is an exception thrown in either the command constructor or the
> connection creation.
> 
> For example if the connection is properly constructed with no 
> exceptions
> then cn is not null and cn.Close() will be closed no matter 
> how the routine
> exits (by return or by exception). If an exception occurs 
> during connection
> creation then cn is null and cn.Close() will not be called.
> 
> The main advantage to using nested using statements is that the using
> statement allows putting your construction and cleanup code 
> in one place (at
> the top of the using block).
> 
> There is a downside though. Each using statement creates a 
> new try...finally
> construct. Look through ILDASM and you will see a 
> try...finally block for
> every nested using statement. While try...finally overhead 
> might be minimal,
> it still requires more overhead than hand coding the 
> try...finally block.
> 
> It would be nice if the using statement allowed 
> initialization of multiple
> types at once! *sigh*
> 
> --
> Peter
> 
> > Kirk Jackson spake:
> >
> > The reason 'using' is useful, is that you don't need to worry
> > about an exception being thrown from your first dispose call (as
> > if there is an exception, you'd still want to call cn.Close)
> >
> > Kirk
> >
> > -----Original Message-----
> > From: Peter Stephens [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, 2 May 2002 8:03 p.m.
> > To: [EMAIL PROTECTED]
> > Subject: Re: [DOTNET] From C# to VB.NET
> >
> >
> > Thank you Mattias,
> >
> > I had never considered that multiple using statements before the
> > code block were valid. I certainly haven't seen it used like this
> > before. Your second technique is also interesting except that it
> > requires the overhead of the cast.
> >
> > My typical pattern is:
> >
> > SqlConnection cn = null;
> > SqlCommand cmd = null;
> > try
> > {
> >         cn = new SqlConnection(...);
> >         cmd = cn.CreateCommand();
> >
> >         // Do something
> > }
> > finally
> > {
> >         if(cmd != null) cmd.Dispose();
> >         if(cn != null) cn.Close();
> > }
> >
> > This keeps everything strongly typed from the start. Still your
> > first technique is intriguing in that it also eliminates the
> > chance of forgetting the cleanup code in the finally block...
> >
> > --
> > Peter
> >
> >
> > > Mattias Sjögren spake:
> > >
> > > Peter,
> > >
> > > >I will often use the try ... finally pattern in C# if there is
> > > more than one
> > > >type of resource to clean up. The using statement only supports
> > > one type at
> > > >a time.
> > >
> > > Yes, but you can easily nest them
> > >
> > > using ( DisposableType1 d1 = new DisposableType1() )
> > > using ( DisposableType2 d2 = new DisposableType2() )
> > > using ( DisposableType3 d3 = new DisposableType3() ) {
> > >    // ...
> > > }
> > >
> > > or
> > >
> > > using ( IDisposable d1 = new DisposableType1(), d2 = new
> > > DisposableType2() ) {
> > >    DisposableType1 dt1 = d1 as DisposableType1;
> > >    DisposableType2 dt2 = d2 as DisposableType2;
> > >    // ...
> > > }
> > >
> > >
> > > Mattias
> >
> > You can read messages from the DOTNET archive, unsubscribe from
> > DOTNET, or subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
> 
> You can read messages from the DOTNET archive, unsubscribe 
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
> 
> You can read messages from the DOTNET archive, unsubscribe 
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
> 

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to