Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2007

Tip: Looking for answers? Try searching our database.

Using Statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barry - 13 Jan 2007 09:43 GMT
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
Alexander Malapheev[MCP] - 13 Jan 2007 10:00 GMT
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.

> Hi
>
[quoted text clipped - 17 lines]
> TIA
> Barry
Michael Nemtsev - 13 Jan 2007 20:44 GMT
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> Barr
ForrestPhoto@gmail.com - 14 Jan 2007 07:08 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.