Variables and Try/Catch

J

Jake K

How do I access variables that are set within a try/catch block? The
following code produces a use of unassigned local variable error:

string varOne;

try
{
//access database here, select, datareader, etc...
varOne = dtr["column_name"].ToString();
}
catch (Exception e)
{
//something
}

MessageBox.Show(varOne);


I want to wrap the database connectivity, etc. in the try catch then use the
variable outside.
 
J

Jake K

P.S. I also have a finally block that disconnects from the db server, etc.
That will not occur if the modal msgbox is still open. So I want to use the
variable set in the try outside of finally.
 
M

Mark Rae

Thanks. Is string varOne = null; the same as string varOne =
String.Empty?

Not strictly speaking - null means having no value at all, and String.Empty
means having a value containing no characters...

In the case of a string variable, this doesn't really matter very much....
 
J

Jake K

Thanks. So string x = null; is fine as well as string x = string.Empty;
or string x = String.Empty;

What are the differences between
string x = null;
string x = string.Empty
string x = String.Empty;

When is it best to use each one?
 
G

Guest

Hi Jake,
What are the differences between
string x = null;
string x = string.Empty
string x = String.Empty;

string is just an alias for System.String, they are the same thing.
Assigning null to a string variable says that the variable does not refer to
any value, whereas String.Empty is the empty string i.e. "" so the string has
a value but it is just an empty string.

For more info on your original problem see:
http://themightycoder.spaces.live.com/blog/cns!EBFBA22CD769E10B!126.entry

Mark
 
J

Jake K

Thanks, Mark. Very informative site.

Mark R. Dawson said:
Hi Jake,


string is just an alias for System.String, they are the same thing.
Assigning null to a string variable says that the variable does not refer
to
any value, whereas String.Empty is the empty string i.e. "" so the string
has
a value but it is just an empty string.

For more info on your original problem see:
http://themightycoder.spaces.live.com/blog/cns!EBFBA22CD769E10B!126.entry

Mark
 
J

Jon Skeet [C# MVP]

Mark Rae said:
Not strictly speaking - null means having no value at all, and String.Empty
means having a value containing no characters...

In the case of a string variable, this doesn't really matter very much....

It matters a great deal! If you try to call any methods on a null
reference, you'll get a NullReferenceException, which you won't if you
call the methods on an empty string. For instance:

if (x.StartsWith ("User")) // Broken if x is null
 
M

Mark Rae

It matters a great deal! If you try to call any methods on a null
reference, you'll get a NullReferenceException, which you won't if you
call the methods on an empty string. For instance:

if (x.StartsWith ("User")) // Broken if x is null

Yes - apologies again - I meant in the context of how the OP was using it,
it wouldn't really matter whether he set his string variable to null or
String.Empty...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Mark Rae said:
Not strictly speaking - null means having no value at all, and
String.Empty means having a value containing no characters...

In the case of a string variable, this doesn't really matter very much....

Quite the opposite, they are completely different situations. assign it to
null means that the variable does not reference any instance. in this case
trying to access any property ( like String.Length) will throw an exception.

Maybe what you mean is
string s = "";
string s= String.Empty;

In this case your post is true, there is no difference.
 
M

Mark Rae

Maybe what you mean is
string s = "";
string s= String.Empty;

Apologies - I should have been clearer. I was referring to the specific
example in the OP:

string varOne;

try
{
//access database here, select, datareader, etc...
varOne = dtr["column_name"].ToString();
}
catch (Exception e)
{
//something
}

In the above specific case, it wouldn't matter very much whether the first
line was:

string varOne = null;

or

string varOne = String.Empty;
 
D

Dave Herron

Declare and initialize the striing before the try/catch. If you don't
initialize the object with value ( null, "", new ..., etc ) it won't be
visible in the catch.

string varOne = null;
or
string varOne = "";

try{}
catch{}
 
T

Tigger

As a side note, I would recommend using the String.IsNullOrEmpty(s)
static method when testing strings. It will pass for null, String.Empty
and "". A lot neater than doing multiple tests every time your about to
use a string.

I often do a Trim() on my strings first incase they consist of a few
spaces.

Tigger


Dave said:
Declare and initialize the striing before the try/catch. If you don't
initialize the object with value ( null, "", new ..., etc ) it won't be
visible in the catch.

string varOne = null;
or
string varOne = "";

try{}
catch{}

Jake said:
How do I access variables that are set within a try/catch block? The
following code produces a use of unassigned local variable error:

string varOne;

try
{
//access database here, select, datareader, etc...
varOne = dtr["column_name"].ToString();
}
catch (Exception e)
{
//something
}

MessageBox.Show(varOne);


I want to wrap the database connectivity, etc. in the try catch then use the
variable outside.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top