The problem is that you don't exit in the catch block, you only show a
message, and then continue on. Add a return after the exit block to ensure
that the unitialized variable won't be accessed.
Alternatively, you can do Utility db = null; -- However, that's not really
your intention, and the compiler can't find mistakes in your code (like this
one -- you'd use the null value later on and get a NullReferenceException).
-Michael
MVP
"Karl" <Ka**@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Hi all,
I have a piece code like this:
static void Main()
{
Utility db;
try
{//create db connection, fill dataset...
db = new Utility();
}
catch(Exception e)
{
MessageBox.Show("Exit...");
}
if( db != null) <--- .net does not like it.
{
Application.Run(new DataForm(db));
}
}
When compiling, .NET complains that db at "if( db != null)" is undefined.
But that what I want to test. How could I get around of this problem?
Thanks,
Karl