Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return type is less accessible than method

Tags:

c#

.net

I am new to c# and here is an excerpt from a personal project i am working on to get some experience.

When calling the getRecipe() function outside this class i am presented with the following error. I want to keep my List private to the CookBook class but still be able to get a reference to one of the Recipes in the List. I do not want to make my List public.

Any advice is greatly appreciated! Thanks


The error

return type 'cookbook.Recipe is less accessible than method 'cookbook.CookBook.getRecipe(string)'

public class CookBook
{
    private List<Recipe> listOfRecipes = new List<Recipe> {};
    public Recipe getRecipe(string name)
    {
        int i = 0;
        while (listOfRecipes[i].getRecipeName() != name)
        {
            i++;
        }
        return listOfRecipes[i];
    }
}
like image 253
prolink007 Avatar asked Jul 24 '11 02:07

prolink007


2 Answers

Make the Recipe class public.

like image 165
CharithJ Avatar answered Nov 15 '22 15:11

CharithJ


Your Recipe class is less accessible than the method. You should check that Recipe is not private/internal and that you can see the Recipe class from outside that class scope (quick fix declare Recipe a public class).

As pointed out by Michael Stum in a comment below classes without an access modifier are by default either internal or private (if it's a nested class). This is possibly where your issue is and you may have just declared class Recipe instead of public class Recipe

like image 11
Jesus Ramos Avatar answered Nov 15 '22 16:11

Jesus Ramos