473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get around this unassigned variable problem?

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
Nov 16 '05 #1
3 1206
Karl <Ka**@discussions.microsoft.com> wrote:
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?


Set db to null to start with:

Utility db = null;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

Nov 16 '05 #3
I read right over the db != null part :S. So it wouldn't throw an exception
later on. My mistake.

Either way still works in this sample though. :)

--
Michael Giagnocavo
MVP
www.atrevido.net

"Michael Giagnocavo [MVP]" <mg*******@atrevido.net> wrote in message
news:O2*************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection...
9
by: Ropo | last post by:
Newbie: I am writing code to read an xml file. I want two values from the document so I want to save them as I am reading through the elements. I want to reference them (using Console.WriteLine)...
12
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just...
3
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
2
by: kaiser | last post by:
Hello I am trying to get the last lines value in a text file and display it on screen / read it into a variable. When I run the following code in a console I get the following error "Use...
29
by: Joseph Geretz | last post by:
Use of unassigned local variable 'fs' Please see where I've indicated where the compiler is flagging this error in the method below. fs is initialized in the first line in the try block, so why...
9
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
22
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.