Home All Groups Group Topic Archive Search About
Author
13 Jan 2007 9:43 AM
Barry
Hi

I recently saw some code written by another programmer in C# , it had code
blocks

using(<some statement to connect to a database>)
{
      using( <some statement for create dataadapter >)
      {
             using ( <some more database statement>)
             {

             }
       }
}

how useful or good is this method, and the code did not have any try ..
catch blocks

TIA
Barry

Author
13 Jan 2007 10:00 AM
Alexander Malapheev[MCP]
Hi Barry,

Code:

    using(SomeType t = new SomeType())
    {
    }

is equivalent to:

    SomeType t = new SomeType();
    try
    {
    }
    finaly
    {
        t.Dispose();
    }

but as you can see when you use "using" statement there are less amount
of code.

In your code using of "using" statement guaranty that database
connection will be closed in any case, dataadapter will be disposed etc.

Also absence of catch blocks means that all exceptions should be catched
in the top of call stack, and it is normal.


Show quote
> Hi
>
> I recently saw some code written by another programmer in C# , it had code
> blocks
>
> using(<some statement to connect to a database>)
> {
>       using( <some statement for create dataadapter >)
>       {
>              using ( <some more database statement>)
>              {
>
>              }
>        }
> }
>
> how useful or good is this method, and the code did not have any try ..
> catch blocks
>
> TIA
> Barry
>
>
>
Author
13 Jan 2007 8:44 PM
Michael Nemtsev
Hello Barry,

What does confuse you?
Alternative way is to use single try{} catch{} and dispose object manually

---
WBR,
Michael  Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

B> Hi
B>
B> I recently saw some code written by another programmer in C# , it had
B> code blocks
B>
B> using(<some statement to connect to a database>)
B> {
B> using( <some statement for create dataadapter >)
B> {
B> using ( <some more database statement>)
B> {
B> }
B> }
B> }
B> how useful or good is this method, and the code did not have any try
B> .. catch blocks
B>
B> TIA
B> Barry
Author
14 Jan 2007 7:08 AM
ForrestPhoto
The only time when this could be a bad thing is if you're writing code
where speed is the utmost importance, above everything else, you put
one try block at the beginning and handle errors inside, with your data
adapter or -reader.  But the using blocks have try/finally built into
them behind the scenes, which basically makes for less code to type
out, or read when you're debugging.

AddThis Social Bookmark Button