1.

How Can I Get Around Scope Problems In A Try/catch?

Answer»

If you try to INSTANTIATE the class inside the try, it'll be out of SCOPE when you try to access it from the catch block. A WAY to get around this is to do the following:
Connection conn = null;
try
{
conn = new Connection();
conn.Open();
}
FINALLY
{
if (conn != null) conn.Close();
}
By setting it to null before the try block, you avoid getting the CS0165 error (Use of possibly unassigned local VARIABLE 'conn').

If you try to instantiate the class inside the try, it'll be out of scope when you try to access it from the catch block. A way to get around this is to do the following:
Connection conn = null;
try
{
conn = new Connection();
conn.Open();
}
finally
{
if (conn != null) conn.Close();
}
By setting it to null before the try block, you avoid getting the CS0165 error (Use of possibly unassigned local variable 'conn').



Discussion

No Comment Found