473,397 Members | 2,099 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,397 software developers and data experts.

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 70146
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
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
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
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: 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
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";
3
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...

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.