Jdi*_*ehl 8 .net c# access-modifiers
我正在为我的学习开发一个应用程序。现在我刚刚启动了一个应用程序,在那里我得到了一个包含足球联赛和俱乐部等的数据库。现在我有了俱乐部和球员的列表,现在我正在尝试添加更多的联赛,然后只有 1 个。但是当我收到这个错误时做同样的事情然后做之前。这是不工作列表的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
然后这是正在工作的俱乐部的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
这是相同的代码,只有俱乐部中的查询使用列表框中的选定项目,但任何人都知道为什么我在第一个列表中收到此错误:
错误 CS0050 不一致的可访问性:返回类型“
List<Competitie>”的可访问性低于方法“DatabaseManager.GetAllCompetities()”
类的代码:
class Competitie
{
public int IdCompetitie { get; set; }
public string NaamCompetitie { get; set; }
public override string ToString()
{
return string.Format("{0}", NaamCompetitie);
}
}
Run Code Online (Sandbox Code Playgroud)
ADy*_*son 15
您必须公开您的课程:
public class Competitie
Run Code Online (Sandbox Code Playgroud)
如果您不指定访问修饰符,则它默认为internal(即,只能在编译成的程序集中访问)。
正如错误所说,该类必须至少与返回它的方法一样可访问。
按照您现在的方式,可以调用 GetAllCompetities() 方法(因为它是公共的)的代码有可能无法访问该方法返回的类。显然这不是合乎逻辑的情况 - 调用代码将无法使用或理解它返回的数据。
注意根据上下文,将 GetAllCompetities() 方法标记为internal与类匹配实际上可能更合适。这样,在程序集之外无法访问任何内容。不过,这完全取决于您的情况和您的需要。我只是为了完整性而注意到这一点。它会解决错误,但会导致这些代码段的可访问性级别不同。
这是 C# 访问修饰符的文档:https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers
| 归档时间: |
|
| 查看次数: |
7391 次 |
| 最近记录: |