问题
Im working on a app for my study. now i just started a app where i got a database with the soccer league's and the clubs etc. now i had a list with the club and the players now im trying to add in more league's then just 1. but i get this error when im doing the same thing then doing before. this is the code of the not working list:
public List<Competitie> GetAllCompetities()
{
List<Competitie> Competitie = new List<Competitie>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = "Select * from competitie";
MySqlCommand selectallcompetitiecommand = new MySqlCommand(query, connection);
MySqlDataReader reader = selectallcompetitiecommand.ExecuteReader();
while (reader.Read())
{
Competitie comp = new Competitie();
comp.IdCompetitie = reader.GetInt32(0);
comp.NaamCompetitie = reader.GetString(1);
Competitie.Add(comp);
}
}
return Competitie;
}
and then this is the code of the clubs this do is working:
public List<Clubs> GetAllClubs(string selecteditem)
{ //Zorgt voor alle dingen van de tabel clubs.
List<Clubs> Clubs = new List<Clubs>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = "Select * from databasevoetbal.clubs where competitie.naamcompetie = '" + selecteditem + "' and clubs.idcompetitie = competitie.idcompetitie";
MySqlCommand selectAllClubsCommand = new MySqlCommand(query, connection);
MySqlDataReader reader = selectAllClubsCommand.ExecuteReader();
while (reader.Read())
{
Clubs Club = new Clubs();
Club.idClubs = reader.GetInt32(0);
Club.NaamClubs = reader.GetString(1);
Club.aantalkampioenschappen = reader.GetInt32(2);
Club.opgericht = reader.GetInt32(3);
Club.idcompetitie = reader.GetInt32(4);
Clubs.Add(Club);
}
}
return Clubs;
}
It's the same code only the query in the club uses a selected item from a listbox but anybody knows why i get this error in the first list:
Error CS0050 Inconsistent accessibility: return type '
List<Competitie>
' is less accessible than method 'DatabaseManager.GetAllCompetities()
'
Code for the class:
class Competitie
{
public int IdCompetitie { get; set; }
public string NaamCompetitie { get; set; }
public override string ToString()
{
return string.Format("{0}", NaamCompetitie);
}
}
回答1:
You must make your class public:
public class Competitie
If you don't specify the access modifier, it defaults to being internal
(i.e.accessible only within the assembly it's compiled into).
As the error says, the class must be at least as accessible as the method which returns it.
The way you've got it now, there's a chance that code which can call the GetAllCompetities() method (because it's public) cannot access the class which the method returns. And clearly that is not a logical situation - the calling code would not be able use or understand the data it gets back.
N.B. Depending on the context, it might actually be more appropriate instead to mark the GetAllCompetities() method as internal
to match the class. That way none of it is accessible outside the assembly. It's entirely dependent on your situation and what you need, though. I'm just noting that for completeness. It would resolve the error, but result in a different level of accessibility for these pieces of code.
Here is the documentation for C# access modifiers: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers
来源:https://stackoverflow.com/questions/53761584/c-sharp-inconsistent-accessibility-return-type-is-less-accessible-than-method