473,396 Members | 1,834 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,396 software developers and data experts.

What is wrong with this?

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'

Aug 20 '07 #1
6 1191
Mick Walker wrote:
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<stringmyMLst = new List<string>();
Aug 20 '07 #2
Mick Walker wrote:
[...]
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
Aug 20 '07 #3
Try adding braces:

if (row["EmailAddress"] != DBNull.Value)
{
eAddress = (string)row["EmailAddress"];
myMLst.Add(eAddress );
}
Mick Walker wrote:
>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<stringmyMLst = new List<string>();

Aug 20 '07 #4
To simplify:
string address = String.Empty

Now "address" is not unassigned.
Cheers,
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Mick Walker" wrote:
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'

Aug 20 '07 #5
Peter Bromberg [C# MVP] wrote:
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" wrote:
>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'

Aug 20 '07 #6
Ebbe Kristensen wrote:
Peter Bromberg [C# MVP] wrote:
>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.
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
Aug 20 '07 #7

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

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.