C# compile error: “X is inaccessible due to its protection level”

僤鯓⒐⒋嵵緔 提交于 2019-11-27 08:08:47

问题


when c# gives this compile error?

'Favorite.Favorites.FavoriteCollection' is inaccessible due to its protection level

private void Form1_Load(object sender, EventArgs e)
{
    Favorites objFavorites = new Favorites(); 

    objFavorites.ScanFavorites();
    foreach (WebFavorite objWebFavorite in objFavorites.FavoriteCollection)
    {
        ListViewItem objListViewItem = new ListViewItem();
        objListViewItem.Text = objWebFavorite.Name;
        objListViewItem.SubItems.Add(objWebFavorite.Url);
        lstFavorites.Items.Add(objListViewItem);
    }
}

回答1:


This compile-time error means that the property you are trying to access is not public and the only way to access it is by either modifying its access modifier or using reflection.




回答2:


When it's not visible enough to reach: If, for example, the class is in another project and the visibility is interal or lower (protected or private), you won't be able to use it. You'll have to change it to public in such a case:

public class FavoriteCollection
{
...
}


来源:https://stackoverflow.com/questions/3595380/c-sharp-compile-error-x-is-inaccessible-due-to-its-protection-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!