Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while create a list method
method name says : inconsistent accessibility

What I have tried:

public List<category> Addcat()//this is the error while write this code
    {
    string qry = "select * from category";
    List<category> list = new List<category>();
    con.Open();
        {
        SqlCommand cmd = new SqlCommand(qry, con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
            {
            category cat = new category();
            cat.id = Convert.ToInt32(dr[0]);
            cat.nam = dr[1].ToString();
            }
        dr.Close();
        con.Close();
        }
    return list;
    }
Posted
Updated 26-Feb-19 1:37am
v2

1 solution

Don't post code as a solution - that removes your question from the "unanswered" list and make it less likely to be looked at. I moved your code into the question, and deleted your "answer"

"Inconsistent accessibility" means that you are trying to add something which is declared as more available than the class that contains it.

For example, a internal class with a public method:
C#
internal class MyClass
   {
   public void MyMethod (){}
   }
Because the class is internal it is only accessible within files in the same assembly, outside the assembly it can't be used or even referenced at all.
But MyMethod is public, which means it can be used anywhere at all. So how can anyone use it? They can access MyMethod, but not declare or use a variable of type MyClass - which means Mymethod is a public method that can't be used outside the same assembly!

And that's what the error message is saying: "Inconsistent accessibility": "the accessibility of the method is greater than the accessibility of the class that contains it so it can't be used as you might expect"

Change your class to public, or change your method to match (or be less accessible than) the class.
 
Share this answer
 
v2
Comments
Richard Deeming 26-Feb-19 10:59am    
I think you might need some more coffee! :)

There's nothing wrong with an internal class having a public method. Code which can't access the class can't access the method either.

The problem arises when, for example, you have a public method of a public class which exposes an internal class:
internal class Foo 
{
    public void MyMethod() { } // No error here
}

public class Bar
{
    public Foo Bar() { } // Error here
    public IEnumerable<Foo> Bars(); // Error here
    public void Bah(Foo foo) { } // Error here
    public void Bah(IEnumerable<Foo> foo) { } // Error here
    public void Baa<T>(T foo) where T : Foo { } // Error here
}
OriginalGriff 26-Feb-19 11:05am    
I''m fine on coffee now, but I wasn't then ... :sigh: I blame the Alzheimer's ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900