簡體   English   中英

C# 不一致的可訪問性:返回類型的可訪問性低於方法

[英]C# Inconsistent accessibility: return type is less accessible than method

我正在為我的學習開發一個應用程序。 現在我剛剛啟動了一個應用程序,在那里我得到了一個包含足球聯賽和俱樂部等的數據庫。現在我有了俱樂部和球員的列表,現在我試圖添加更多的聯賽,然后只有 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;
        }

然后這是正在工作的俱樂部的代碼:

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

這是相同的代碼,只有俱樂部中的查詢使用列表框中的選定項目,但任何人都知道為什么我在第一個列表中收到此錯誤:

錯誤 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);
        }
    } 

您必須公開您的課程:

public class Competitie

如果您不指定訪問修飾符,則默認為internal (即只能在編譯成的程序集中訪問)。

正如錯誤所說,該類必須至少與返回它的方法一樣可訪問。

按照您現在的方式,可以調用 GetAllCompetities() 方法(因為它是公共的)的代碼有可能無法訪問該方法返回的類。 顯然這不是合乎邏輯的情況 - 調用代碼將無法使用或理解它返回的數據。

注意根據上下文,將 GetAllCompetities() 方法標記為internal以匹配類實際上可能更合適。 這樣,在程序集之外無法訪問任何內容。 不過,這完全取決於您的情況和您的需要。 我只是為了完整性而注意到這一點。 它會解決錯誤,但會導致這些代碼段的可訪問性級別不同。

這是 C# 訪問修飾符的文檔: https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM