未分配的局部变量

时间:2014-04-17 20:46:16

标签: c#

我打赌这些类型的问题是最常见的,但是在尝试了其他几个问题并且仍然出错之后,我来到了这里。我收到以下错误:

  

使用未分配的局部变量'nodeRss'
  使用未分配的局部变量'nodeChannel'

class Program
{
    static void Main(string[] args)
    {

        XmlTextReader rssReader;
        XmlDocument rssDoc;
        XmlNode nodeRss;
        XmlNode nodeChannel;
        String title;
        String text;
        HttpWebRequest http = WebRequest.Create("http://www.aerolitegaming.com/login/login") as HttpWebRequest;
        http.KeepAlive = true;
        http.Method = "POST";
        http.AllowAutoRedirect = true;
        http.ContentType = "application/x-www-form-urlencoded";
        string postData="login=SNIP&register=0&password=SNIP&remember=1&cookie_check=0&redirect=forum%2F&_xfToken=";
        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
        http.ContentLength = dataBytes.Length;
        using (Stream postStream = http.GetRequestStream())
        {
            postStream.Write(dataBytes, 0, dataBytes.Length);
        }
        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
        int y = (int)httpResponse.StatusCode;
        http = WebRequest.Create("http://www.aerolitegaming.com/forum") as HttpWebRequest;
        http.CookieContainer = new CookieContainer();
        http.CookieContainer.Add(httpResponse.Cookies);
        http.AllowAutoRedirect=false;
        HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
        rssReader = new XmlTextReader("http://aerolitegaming.com/forums/in-game-reports.132/index.rss");
        rssDoc = new XmlDocument();
        rssDoc.Load(rssReader);
        // Loop for the <rss> tag
        for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
        {
            // If it is the rss tag
            if (rssDoc.ChildNodes[i].Name == "rss")
            {
                // <rss> tag found
                nodeRss = rssDoc.ChildNodes[i];
            }
        }
        // Loop for the <channel> tag
        for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
        {
            // If it is the channel tag
            if (nodeRss.ChildNodes[i].Name == "channel")
            {
                // <channel> tag found
                nodeChannel = nodeRss.ChildNodes[i];
            }
        }
        // Set the labels with information from inside the nodes
        title = "Title: " + nodeChannel["title"].InnerText;
        text = "Description: " + nodeChannel["description"].InnerText;
        Console.WriteLine(title);
        Console.WriteLine(text);
    }
}

3 个答案:

答案 0 :(得分:3)

nodeRss变量是if语句中分配的:

if (rssDoc.ChildNodes[i].Name == "rss")
{
     // <rss> tag found
     nodeRss = rssDoc.ChildNodes[i];
}

我确信你永远不会忘记if语句,所以你不担心缺少初始化。但是,编译器不知道这一点,因此抱怨nodeRss从未被分配(因为它不能保证)。

实际上,我非常怀疑你实际上是否确保进入if语句,所以你应该为它分配一个默认值(null就可以了)并在使用变量之前检查该值。

nodeChannel遇到了同样的问题。

答案 1 :(得分:1)

因为您将这些变量值分配到不同的范围{}。

要阻止消息,只需给它们值。

 XmlNode nodeRss = null;
 XmlNode nodeChannel = null;

答案 2 :(得分:0)

看起来你只是在某些条件下为这两个变量赋值,如果不满足这些条件,就会导致它们没有被设置。