Erica Stockwell-Alpert
Erica Stockwell-Alpert

Reputation: 4863

Public class is inaccessible due to protection level; Constructor is public

I have a class that is public, ArticleDao, but when I try to use it in another file, it says "ArticleDao is inaccessible due to its protection level." Here is the entire class:

class ArticleDao : IArticleDao
{
    private readonly ContentManager _contentManager;

    private static readonly char[] DelimiterChars = { '|', '\n' };

    private const int ArticlePagingSize = 500;

    public ArticleDao()
    {
        _contentManager = new ContentManager();
    }

    private Image GetImage(XElement element)
    {
        var image = new Image();

        if (String.IsNullOrEmpty((String)element))
        {
            return image;
        }

        XElement imageElement = element.Element("img");
        if (imageElement != null)
        {
            image.Url = (String)imageElement.Attribute("src");
        }
        return image;
    }

    private Link GetLink(XElement element)
    {
        var link = new Link();

        if (String.IsNullOrEmpty((String)element))
        {
            return link;
        }

        XElement anchorElement = element.Element("a");
        if (anchorElement != null)
        {
            link.Url = (String)anchorElement.Attribute("href");
            link.Text = (String)anchorElement;
        }
        return link;
    }

    public Article GetArticle(long id, string html)
    {
        var a = new Article();
        long testid = 556;
        if (id == testid)
        {
            var x = 1;
        }

        XDocument xdoc = XDocument.Parse(html);
        var xElement = xdoc.Element("root");
        if (xElement != null)
        {
            XElement articleElem = xElement.Element("Event");
            if (articleElem != null)
            {
                a = new Article();
                a.Id = id.ToString(CultureInfo.InvariantCulture);
                a.Title = (String)articleElem.Element("Title");
                a.PublishedDate = GetDateTime((String)articleElem.Element("PublishedDate"));
                a.SubHeader = (String)articleElem.Element("SubHeader");
                a.Image = GetImage(articleElem.Element("Image"));
                a.Caption = (String)articleElem.Element("Caption");
                a.Body = (String)articleElem.Element("Body");
                a.Url = GetLink(articleElem.Element("Url"));

            }
        }

        return a;

    }

    public Article GetArticle(Int64 ektronContentId)
    {
        var item = _contentManager.GetItem(ektronContentId);
        return GetArticle(ektronContentId, item.Html);
    }

    public IEnumerable<Article> GetArticles(Int64 folderId)
    {
        int count;
        IEnumerable<Article> articles = new List<Article>(ArticlePagingSize);
        do
        {
            var criteria = new ContentCriteria();
            criteria.AddFilter(ContentProperty.FolderId, CriteriaFilterOperator.EqualTo, folderId);
            criteria.PagingInfo.RecordsPerPage = ArticlePagingSize;

            var articleContentData = _contentManager.GetList(criteria);
            count = articleContentData == null ? 0 : articleContentData.Count;

            articles = articles.Concat(_contentManager.GetList(criteria)
                .Select(i => GetArticle(i.Id, i.Html)));
        } while (count == ArticlePagingSize);
        return articles;
    }

    private DateTime GetDateTime(string date)
    {
        DateTime dt;

        DateTime.TryParse(date, out dt);

        return dt;
    }
}

The constructor is public. I even tried replacing all instances of "private" with "public," but it still says that it is inaccessible. This is the line where I'm trying to invoke it:

private static IArticleDao _articleDao;
public static IArticleDao ArticleDao
{
    get { return _articleDao ?? (_articleDao = new ArticleDao()); }
}

Where it says "_articleDao = new ArticleDao()" is where the error is.

I'm especially confused because to create ArticleDao and IArticleDao, I essentially just copied EventDao and IEventDao and replaced "Event" with "Article." This works:

private static IEventDao _eventDao;
public static IEventDao EventDao
{
    get { return _eventDao ?? (_eventDao = new EventDao()); }
}

but ArticleDao does not work.

Upvotes: 0

Views: 2563

Answers (3)

faester
faester

Reputation: 15086

The class' default access level is internal. Internal types or members are accessible only within files in the same assembly.

You probably want to specify

public class ArticleDao...

The constructor accessibility is not the same as the class access level, and if something is hidden by the class access modifier you cannot access any of its members regardless of their access modifier.

Upvotes: 0

Tigran
Tigran

Reputation: 62265

You class ArticleDao is a internal, as if you don't specify any accessibility modifier, the default in C# is internal. To resolve this issue you may think of declaring it public

public class ArticleDao : IArticleDao
{
   ...
}

Upvotes: 2

Servy
Servy

Reputation: 203821

The class itself isn't public. It's internal. (The default accessibility for anything is the smallest legal accessibility option, which for a non-nested class, is internal. You have specified no accessibility option explicitly.)

Upvotes: 3

Related Questions