Unable to access protected method in derived class

Viewed 1972

I don't understand why this code gives me an Intellisense error.

public abstract class Node
{
    protected abstract string ToText();
}

public class HtmlNode : Node
{
    public List<Node> ChildNodes { get; set; }

    protected override string ToText()
    {
        StringBuilder builder = new StringBuilder();

        foreach (var node in ChildNodes)
            builder.Append(node.ToText());  // <=== THIS IS THE ERROR

        return builder.ToString();
    }
}

On the line indicated above, I get the error:

Error CS1540: Cannot access protected member 'Node.ToText()' via a qualifier of type 'Node'; the qualifier must be of type 'HtmlNode' (or derived from it)

HtmlNode derives from Node, so why can't HtmlNode access protected members of Node?

And how would I modify the code to use "a qualifier of type HtmlNode", as suggested in the error message?

2 Answers
Related