willa
willa

Reputation: 669

not all code paths return a value using html agility pack

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.

Upvotes: 0

Views: 151

Answers (6)

Tim Schmelter
Tim Schmelter

Reputation: 460158

The method's promises to return a IList<Category> here:

private IList<Category> GetFeatureSubCategories

So it has to return it in any way(or at least null which is the default value).

But you don't return a list. So just add return categories; at the end.

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:

Methods with a non-void return type are required to use the return keyword to return a value.

Upvotes: 1

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

you are not returing categories any where in the code

Add return statement at the end of your code like i hv added

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

Upvotes: 0

DGibbs
DGibbs

Reputation: 14618

Your method retuns a IList<Category> but you aren't returning an IList<Category> anywhere in your code. Call: -

return categories;

Upvotes: 0

TalentTuner
TalentTuner

Reputation: 17556

You are not returning anything from a method which declare to return ILIst

add return categories; at after second last '}' bracket

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151604

The error is pretty self-explanatory: your code does not return anything, while the method's signature promises it does.

return categories; at the end of the method will do.

Upvotes: 1

Habib
Habib

Reputation: 223277

Your method is suppose to return an object of type IList<Category>, you don't have return statement anywhere in your code. Probably you want to return categories from your method, you can place the return statement just before the method end.

private IList<Category> GetFeatureSubCategories(HtmlNode std, Category category)
{
        List<Category> categories = new List<Category>();
        {
        //.................
return categories;
}

Upvotes: 1

Related Questions