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.