Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:25:35+00:00 2026-06-15T06:25:35+00:00

Consider you have the following code: public abstract class MenuItem { protected string m_Title;

  • 0

Consider you have the following code:

public abstract class MenuItem
    {
        protected string m_Title;
        protected int m_Level;
        protected MenuItem m_ParentItem;
        public event ChooseEventHandler m_Click;

        protected MenuItem(string i_Title, int i_Level, MenuItem i_ParentItem)
        {
            m_Title = i_Title;
            m_Level = i_Level;
            m_ParentItem = i_ParentItem;
        }
}

and

public class ContainerItem : MenuItem
    {
    private List<MenuItem> m_SubMenuItems;

    public ContainerItem(string i_Title, int i_Level, MenuItem i_ParentItem)
                            :base(i_Title, i_Level, i_ParentItem)
    {
        m_SubMenuItems = new List<MenuItem>();
    }

    public string GetListOfSubItems()
    {
        string subItemsListStr = string.Empty;

        foreach (MenuItem item in m_SubMenuItems)
        {
           item.m_Title = "test";  // Cannot access protected member the qualifier   
                                  must be of type 'Ex04.Menus.Delegates.ContainerItem' 

        }

        return subItemsListStr;
    }
}
  1. I really do not understand the logic behind this error, and yes I have already read:
    http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/491031.aspx
    But I still see it totally illogical according to the definition of Protected Access modifier.
    I see it as should be accessible from the same class where it was defined which is MenuItem and for all its derived classes! (ContainerItem ,etc)

  2. How would you access the protected members like m_Title while holding a reference to MenuItem (because of Polymorphism design reasons)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:25:36+00:00Added an answer on June 15, 2026 at 6:25 am

    Why does this happen?

    An answer that cannot be argued with is “because the spec says so“:

    A protected member of a base class is accessible in a derived class
    only if the access occurs through the derived class type.

    But let’s explore this restriction behind the scenes.

    Explanation

    What happens here is the same thing that Eric Lippert describes in the blog post that you linked to. Your code does the equivalent of this:

    public abstract class MenuItem
    {
        protected string m_Title;
    }
    
    public class ContainerItem : MenuItem
    {
        void Foo()
        {
            var derivedItem = new ContainerItem();
            derivedItem.m_Title = "test"; // works fine
    
            var baseItem = (MenuItem)derived;
            baseItem.m_Title = "test"; // compiler error!
        }
    }
    

    The problem here stems from the fact that this might happen. For the moment, please disregard the fact that this example uses a method instead of a field — we ‘ll come back to it.

    public abstract class MenuItem
    {
        protected void Foo() {}
    }
    
    public class SomeTypeOfItem : MenuItem
    {
        protected override void Foo() {}
    }
    
    public class ContainerItem : MenuItem
    {
        void Bar()
        {
            var baseItem = (MenuItem)something;
            baseItem.Foo(); // #1
        }
    }
    

    Look at line #1: how does the compiler know that baseItem is not actually a SomeTypeOfItem? If it is, you certainly must not be able to access Foo! So, as Eric describes, the compiler is unable to statically prove that the access is always legal and because of that it has to disallow this code.

    Note that in some cases, for example if

    baseItem = (MenuItem)new ContainerItem();
    

    or even

    baseItem = (MenuItem)this;
    

    the compiler does have enough information to prove that the access is legal but it still will not allow the code to compile. I imagine that’s because the compiler team is not convinced that implementing such special-case handlers is worth the trouble (a point of view which I am sympathetic to).

    But… but…

    That’s all well and good for methods (and properties, which are really methods) — what about fields? What about this:

    public abstract class MenuItem
    {
        protected string m_Title;
    }
    
    public class SomeTypeOfItem : MenuItem
    {
        protected new string m_Title;
    }
    
    public class ContainerItem : MenuItem
    {
        void Foo()
        {
            var baseItem = (MenuItem)something;
            baseItem.m_Title = "Should I be allowed to change this?"; // #1
        }
    }
    

    Since fields cannot be overridden, there should be no ambiguity here and the code should compile and set MenuItem.m_Title irrespective of what the type of something is.

    Indeed, I cannot think of a technical reason why the compiler couldn’t do this, but there is a good reason in any case: consistency. Eric himself would probably be able to provide a richer explanation.

    So what can I do?

    How would you access the protected members like m_Title while holding
    a reference to MenuItem (because of Polymorphism design reasons)?

    You simply cannot do that; you would have to make the members internal (or public).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about .net generics. Consider the following code: public abstract class
Consider the following code: [Serializable] public class Human { public string Name { get;
Consider the following class hierarchy: Abstract class Printer{ public print(){ //code to handle printing
Consider the following code: class C { public int A { get; set; }
Consider the following code snippet: class Test { public int Length{ get; set; }
I have the following snippet of code. abstract class MrParent { public function __construct()
Consider the following code: class Sample{ public static void main(String args[]){ String text1=C:\Documents\User\sample; String
Consider the following code: public abstract class Test1 { public object Data { get;
Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
Consider the following code. public interface IFoo { } public class Bar { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.