use of unassigned local variable

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Ropo (Ropo_at_discussions.microsoft.com)
Date: 09/28/04


Date: Tue, 28 Sep 2004 13:03:09 -0700

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);
}



Relevant Pages

  • Re: reduce()--what is it good for? (was: Re: reduce() anomaly?)
    ... | minimal set of controls the same applies to writing code as well. ...
    (comp.lang.python)
  • Re: strong/weak typing and pointers
    ... Those automatic coercions are always handy when writing code, ... pain in the @$$ when reading it. ...
    (comp.lang.python)
  • Re: use of unassigned local variable
    ... WriteLine(Msg); without MSg ever being assigned to. ... in a proper XML file, but the compile doesn't know that. ... > I am writing code to read an xml file. ... > catch (Exception e) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Some comments on new 1.9 features
    ... > and one’d have to be able to stand reading both ways of writing code. ... Prev by Date: ...
    (comp.lang.ruby)
  • Re: Try...Catch... Resume??
    ... I'm trying to accomplish is basically ignore an element error when reading ... I'm only interested in certain parts of the XML file. ... Using the preceding exception handling you cannot return to the loop to ...
    (microsoft.public.dotnet.general)