4
 namespace PalleTech
 {          
     public class Parent
        {
            private int test = 123;
            public virtual int TestProperty
            {
                // Notice the accessor accessibility level.
                 set {
                    test = value;
                }

                // No access modifier is used here.
               protected get { return test; }
            }
        }
        public class Kid : Parent
        {
            private int test1 = 123;
            public override int TestProperty
            {
                // Use the same accessibility level as in the overridden accessor.
                 set { test1 = value / 123; }

                // Cannot use access modifier here.
               protected get { return 0; }
            }
        }
        public class Demo:Kid
        {
            public static void Main()
            {
                Kid k = new Kid();
                Console.Write(k.TestProperty);

            }
        }
    }

Error 1 Cannot access protected member 'PalleTech.Parent.TestProperty' via a qualifier of type 'PalleTech.Kid'; the qualifier must be of type 'PalleTech.Demo' (or derived from it)

default
  • 11,485
  • 9
  • 66
  • 102
ravikumar
  • 41
  • 1
  • possible duplicate of [Is there a way to reach a \`protected\` member of another object from a derived type?](http://stackoverflow.com/questions/344503/is-there-a-way-to-reach-a-protected-member-of-another-object-from-a-derived-ty), or [this](http://stackoverflow.com/questions/567705/why-cant-i-access-c-sharp-protected-members-except-like-this), or [this](http://stackoverflow.com/questions/10657545/c-sharp-accessing-protected-member-in-derived-class)... – Rawling Jan 24 '13 at 12:03
  • @bAN why did you increase the indenting so much? – default Jan 24 '13 at 12:14

3 Answers3

3

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

Here you are accessing Kid's protected setter with its instance. You must create instance of Demo class and access via it.

aaa
  • 1,114
  • 12
  • 19
1

Getter of TestProperty in Kid class is protected meaning that if you write a class deriving from Kid class you can access TestProperty; if you create an instance of Kid class you can not access it.

You can change the behaviour by removing protected from setters of both classes;

public class Parent
{
    private int test = 123;
    public virtual int TestProperty
    {
        // Notice the accessor accessibility level.
        set
        {
            test = value;
        }

        // No access modifier is used here.
        get { return test; }
    }
}
public class Kid : Parent
{
    private int test1 = 123;
    public override int TestProperty
    {
        // Use the same accessibility level as in the overridden accessor.
        set { test1 = value / 123; }

        // Cannot use access modifier here.
        get { return 0; }
    }
}
daryal
  • 14,643
  • 4
  • 38
  • 54
0

You must set the assessor to protected too. The getter/setter cannot have a less restricted access modifier than the property itself.

default
  • 11,485
  • 9
  • 66
  • 102
Miguel Matos
  • 191
  • 7