0

I am rather new to c# and the html agility pack I have wrote this code to parse apart of a webpage.

private IList<Category> GetFeatureSubCategories(HtmlNode std, Category category)
    {
        List<Category> categories = new List<Category>();
        {
            if (category.name == "Featured")
            {
                var nodes = std.SelectNodes("//span[contains(@class,'widget')] [position() <= 4]");

                foreach (var node in nodes)
                {
                    string name = SiteParserUtilities.ParserUtilities.CleanText(System.Net.WebUtility.HtmlDecode(node.InnerText));
                    string url = node.Attributes["href"].Value;
                    string identifier = url.Split('/').Last().Replace(".html", "");
                    WriteQueue.write(string.Format(" Category [{0}].. {1} ", name, url));

                    IList<Category> sub = GetSubCategories(std);
                    Category c = new Category()
                    {
                        active = true,
                        Categories = sub.ToArray(),
                        description = "",
                        identifier = identifier,
                        name = name,
                        Products = new Product[0],
                        url = url,
                    };
                    StatisticCounters.CategoriesCounter();
                    categories.Add(c);
                }


            }

        }

    }

I am receiving an error message saying "SiteParser.GetFeatureSubCategories(HtmlAgilityPack.HtmlNode, Category)': not all code paths return a value" I was just wondering whether anyone would be able to give me some advice in why this error message is occurring. Thanks for any help you can offer.

4

6 回答 6

1

您的方法假设返回 type 的对象IList<Category>,您的代码中没有任何return语句。可能您想categories从您的方法返回,您可以将 return 语句放在方法结束之前。

private IList<Category> GetFeatureSubCategories(HtmlNode std, Category category)
{
        List<Category> categories = new List<Category>();
        {
        //.................
return categories;
}
于 2013-03-21T10:21:38.077 回答
1

该错误是不言自明的:您的代码什么都不做return,而方法的签名承诺它会做。

return categories;在方法结束时会做。

于 2013-03-21T10:22:08.153 回答
1

该方法的承诺在IList<Category>这里返回:

private IList<Category> GetFeatureSubCategories

所以它必须以任何方式返回它(或者至少null是默认值)。

但是你没有返回一个列表。所以只return categories;在最后加上。

private IList<Foo> GetFeatureSubCategories(HtmlNode std, Foo category)
{
    List<Category> categories = new List<Category>();
    {
        if (category.Name == "Featured")
        {
            var nodes = std.SelectNodes("//span[contains(@class,'widget')] [position() <= 4]");
            foreach (var node in nodes)
            {
               // blah ...
            }
            // blah ...
        }
    }
    return categories;
}

MSDN

具有非 void 返回类型的方法需要使用 return 关键字来返回值。

于 2013-03-21T10:24:59.743 回答
0

您没有从声明返回 ILIst 的方法返回任何内容

return categories;在倒数第二个'}'括号之后添加

于 2013-03-21T10:22:18.517 回答
0

您的方法返回 aIList<Category>但您没有返回IList<Category>代码中的任何位置。称呼: -

return categories;
于 2013-03-21T10:23:31.557 回答
0

您不会在代码中的任何地方重新分类

在代码末尾添加 return 语句,就像我添加的一样

private IList<Category> GetFeatureSubCategories(HtmlNode std, Category category)
{
    List<Category> categories = new List<Category>();
    {
        if (category.name == "Featured")
        {
            var nodes = std.SelectNodes("//span[contains(@class,'widget')] [position() <= 4]");

            foreach (var node in nodes)
            {
                string name = SiteParserUtilities.ParserUtilities.CleanText(System.Net.WebUtility.HtmlDecode(node.InnerText));
                string url = node.Attributes["href"].Value;
                string identifier = url.Split('/').Last().Replace(".html", "");
                WriteQueue.write(string.Format(" Category [{0}].. {1} ", name, url));

                IList<Category> sub = GetSubCategories(std);
                Category c = new Category()
                {
                    active = true,
                    Categories = sub.ToArray(),
                    description = "",
                    identifier = identifier,
                    name = name,
                    Products = new Product[0],
                    url = url,
                };
                StatisticCounters.CategoriesCounter();
                categories.Add(c);
            }
        }
    }
         return categories;
}
于 2013-03-21T10:23:32.997 回答