473,499 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use of unassigned local variable

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) after I complete the loop. When I
compile I get the "use of unassigned local variable".

What am I doing wrong?

stripped down code:
static void Main()
{
string Msg;

try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("...");

while ( reader.Read() )
{
if (reader.NodeType == XmlNodeType.Whitespace)
continue;

if (reader.NodeType != XmlNodeType.EndElement )
{
Msg = reader.Value;
}

if (reader.HasAttributes)
{
reader.MoveToFirstAttribute();
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

Console.WriteLine(Msg);
}
Nov 16 '05 #1
9 70154
Not sure if this will help, but I have noticed in some cases that when I
define a variable in the function like that and use try, catch, or foreach
statements, it seems to loose scope. Assuming Msg is what is having the
problem during compile time, put it up in the definitions of the controls on
the page, or right under the class definition line and see if that works.

"Ropo" <Ro**@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
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) after I complete the loop. When I compile I get the "use of unassigned local variable".

What am I doing wrong?

stripped down code:
static void Main()
{
string Msg;

try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("...");

while ( reader.Read() )
{
if (reader.NodeType == XmlNodeType.Whitespace)
continue;

if (reader.NodeType != XmlNodeType.EndElement )
{
Msg = reader.Value;
}

if (reader.HasAttributes)
{
reader.MoveToFirstAttribute();
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

Console.WriteLine(Msg);
}

Nov 16 '05 #2
Ropo <Ro**@discussions.microsoft.com> wrote:
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) after I complete the loop. When I
compile I get the "use of unassigned local variable".

What am I doing wrong?


<snip>

Well, consider the situation where an exception is thrown. It may be
thrown before Msg is ever assigned to - so what do you expect

Console.WriteLine (Msg);

at the end to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Thanx, that helped.

I added 'static string Msg;' below the class definition and the value is
retrain.
"Paul Yanzick" wrote:
Not sure if this will help, but I have noticed in some cases that when I
define a variable in the function like that and use try, catch, or foreach
statements, it seems to loose scope. Assuming Msg is what is having the
problem during compile time, put it up in the definitions of the controls on
the page, or right under the class definition line and see if that works.

"Ropo" <Ro**@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
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) after I complete the loop. When

I
compile I get the "use of unassigned local variable".

What am I doing wrong?

stripped down code:
static void Main()
{
string Msg;

try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("...");

while ( reader.Read() )
{
if (reader.NodeType == XmlNodeType.Whitespace)
continue;

if (reader.NodeType != XmlNodeType.EndElement )
{
Msg = reader.Value;
}

if (reader.HasAttributes)
{
reader.MoveToFirstAttribute();
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

Console.WriteLine(Msg);
}


Nov 16 '05 #4
the writeline is just a test to see if I can get the values at the end of the
loop. the main purpose is to send the Msg to an IM.

thanx

"Jon Skeet [C# MVP]" wrote:
Ropo <Ro**@discussions.microsoft.com> wrote:
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) after I complete the loop. When I
compile I get the "use of unassigned local variable".

What am I doing wrong?


<snip>

Well, consider the situation where an exception is thrown. It may be
thrown before Msg is ever assigned to - so what do you expect

Console.WriteLine (Msg);

at the end to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Ropo <Ro**@discussions.microsoft.com> wrote:
the writeline is just a test to see if I can get the values at the end of the
loop. the main purpose is to send the Msg to an IM.


But in the code you've given, it's the WriteLine which is the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Basically, the error is says that it is possible for it to reach the
WriteLine(Msg); without MSg ever being assigned to. This would happen if
the first element was of type EndElement. Of course, that couldn't happen
in a proper XML file, but the compile doesn't know that. The simplest
solution is to just assign a value to Msg before you start.
string Msg = "";

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Ropo" <Ro**@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com... 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) after I complete the loop. When I compile I get the "use of unassigned local variable".

What am I doing wrong?

stripped down code:
static void Main()
{
string Msg;

try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("...");

while ( reader.Read() )
{
if (reader.NodeType == XmlNodeType.Whitespace)
continue;

if (reader.NodeType != XmlNodeType.EndElement )
{
Msg = reader.Value;
}

if (reader.HasAttributes)
{
reader.MoveToFirstAttribute();
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

Console.WriteLine(Msg);
}

Nov 16 '05 #7
On Tue, 28 Sep 2004 13:03:09 -0700, Ropo wrote:
What am I doing wrong?


The C# compiler is very strict.

Consider:

1 static void Main() {
2 string message;
3 if (DateTime.Now.Year == 2004) {
4 message = "It's Y2k!";
5 }
6 Console.WriteLine(message);
7 }

If line 6, message isn't *guaranteed* to be assigned, so the compiler will
produce an error.

The fix could be to change line 2 to:

string message = String.Empty;

Now the compiler knows that message definitely *has* been assigned before
the reference to it in line 6, and won't complain any more.
Nov 16 '05 #8
On Wed, 29 Sep 2004 06:19:36 +0100, C# Learner wrote:
4 message = "It's Y2k!";


In fact, that should be:

message = "It's Y2k+4!";

Or something like that. :-)
Nov 16 '05 #9
Excellent info....thanx in bunches.

"C# Learner" wrote:
On Wed, 29 Sep 2004 06:19:36 +0100, C# Learner wrote:
4 message = "It's Y2k!";


In fact, that should be:

message = "It's Y2k+4!";

Or something like that. :-)

Nov 16 '05 #10

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

Similar topics

3
3934
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...
5
1337
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class,...
3
6459
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
3775
by: Doug | last post by:
I am trying to work with an object and create it like so: EXTRA.Sessions oSessions = new EXTRA.Sessions(); This doesn't work because I get this error: 'Cannot create an instance of the...
9
6171
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
8381
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";
3
3848
by: Hegel | last post by:
Some body please help me. I finds this code really correct. However I am a beginner The following code yields Use of unassigned local variable 'fout' error in my pgm please help using...
2
6415
by: plasmay | last post by:
Hi, I am new in learning c#, and have recently encountered a problem: static float ComputeAvg(float a) { float sum; int i; for (i = 0; i...
3
2481
by: lenniekuah | last post by:
Hi Good Guy, I am very surprised of the coding generate error message Error 1 Use of unassigned local variable 'strSql' even though the strSQL variable was declared. Not sure what is wrong...
0
7130
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
7171
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
7220
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
5468
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,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.