What is wrong with this?

M

Mick Walker

string eAddress;
try
{
adapter.Fill(myDataset);
foreach (DataRow row in myDataset.Tables[0].Rows)
{


if (row["EmailAddress"] != DBNull.Value)
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
}
.... etc etc

I get the error message:

Error 6 Use of unassigned local variable 'eAddress'
 
M

Mick Walker

Mick said:
string eAddress;
try
{
adapter.Fill(myDataset);
foreach (DataRow row in myDataset.Tables[0].Rows)
{


if (row["EmailAddress"] != DBNull.Value)
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
}
... etc etc

I get the error message:

Error 6 Use of unassigned local variable 'eAddress'

Oh and MLst as defined as:
List<string> myMLst = new List<string>();
 
P

Peter Duniho

Mick said:
[...]
I get the error message:

Error 6 Use of unassigned local variable 'eAddress'

Just what the error says. You've used "eAddress" without guaranteeing
to the compiler that you've assigned something to it first. The
compiler has no way to know whether the first time through your loop the
clause in the if() statement will be true or not. Since it might be
false, you might reach the next line without having assigned something
to eAddress and that's an error.

See "definitely assigned" in MSDN for more details.

Pete
 
N

Niels Ull

Try adding braces:

if (row["EmailAddress"] != DBNull.Value)
{
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
Mick said:
string eAddress;
try
{
adapter.Fill(myDataset);
foreach (DataRow row in myDataset.Tables[0].Rows)
{
if (row["EmailAddress"] != DBNull.Value)
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
}
... etc etc
I get the error message:

Error 6 Use of unassigned local variable 'eAddress'
Oh and MLst as defined as:
List<string> myMLst = new List<string>();
 
E

Ebbe Kristensen

Peter said:
To simplify:
string address = String.Empty

Now "address" is not unassigned.

Correct - and sub-optimal because now 'myMLst.Add()' will have to contain a
check for an empty address, a check that already has been performed in the
preceding if-statement.

Ebbe
Cheers,
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Mick Walker said:
string eAddress;
try
{
adapter.Fill(myDataset);
foreach (DataRow row in myDataset.Tables[0].Rows)
{


if (row["EmailAddress"] != DBNull.Value)
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
}
.... etc etc

I get the error message:

Error 6 Use of unassigned local variable 'eAddress'
 
P

Peter Duniho

Ebbe said:
Correct - and sub-optimal because now 'myMLst.Add()' will have to contain a
check for an empty address, a check that already has been performed in the
preceding if-statement.

Why would you say that? The check in the if() statement doesn't check
the variable, it checks the database row. The statement using the
variable executes whether the database row includes that value or not.
It's a bug to execute that line of code without initialization the
variable first.

Now, it may well be a bug to execute the line of code when the database
row's value is missing as well. But that's a separate issue; the code
posted has clear semantics, even if they are wrong. You can't just go
making changes to the basic behavior of the code, not without confirming
with the OP that that's a reasonable thing to do.

Pete
 

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