0

I am learning C#, i written the below code which is not working.

the protected members can be accessble by inheriting the class, but in the below code it is not working can any one please tell me where i am doing mistake?

   class ProtectedDemo
{
    protected string name;

    public void Print()
    {
        Console.WriteLine("Name is: {0}", name);
    }
}

class Demo : ProtectedDemo
{
    static void Main(string[] args)
    {
        ProtectedDemo p = new ProtectedDemo();
        Console.Write("Enter your Name:");
        p.name = Console.ReadLine(); //Here i am getting the error.
        p.Print();
        Console.ReadLine();
    }
}
user1794624
  • 73
  • 2
  • 6
  • What error are you getting? Is it a syntax error or a runtime error for example? – Greg the Incredulous Dec 02 '14 at 05:42
  • 1
    `p` is not inherited from `ProtectedDemo` – bansi Dec 02 '14 at 05:43
  • I am getting the below error:Cannot access protected member 'AccessSpecifiers.ProtectedDemo.name' via a qualifier of type 'AccessSpecifiers.ProtectedDemo'; the qualifier must be of type 'AccessSpecifiers.Demo' (or derived from it) – user1794624 Dec 02 '14 at 05:46
  • Why does this happen? see : [Here][1] [1]: http://stackoverflow.com/questions/13683324/cannot-access-protected-member-in-base-class – programmer Dec 02 '14 at 05:46

3 Answers3

2

From protected (C# Reference)

The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.

So from this comment, it is accessible from within ProtectedDemo or from the inheriting class Demo

And then their example

class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        // Error CS1540, because x can only be accessed by
        // classes derived from A.
        // a.x = 10; 

        // OK, because this class derives from A.
        b.x = 10;
    }
}

So change you class to

class Demo : ProtectedDemo
{
    static void Main(string[] args)
    {
        //ProtectedDemo p = new ProtectedDemo();
        Demo p = new Demo(); //NOTE HERE
        Console.Write("Enter your Name:");
        p.name = Console.ReadLine(); //Here i am getting the error.
        p.Print();
        Console.ReadLine();
    }
}
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

Protected is only accessible for deriving classes or the actual class itself.

Here is an article about access modifiers: What are Access Modifiers in C#?

If you want to set it, either make it public or make a method that takes a string as a parameter and set it there. Something like this.

    class ProtectedDemo
    {
        protected string name;

        public void Print()
        {
            Console.WriteLine("Name is: {0}", name);
        }

        public void SetName(string newName)
        {
            name = newName;
        }
    }

    static void Main(string[] args)
    {
        ProtectedDemo p = new ProtectedDemo();
        Console.Write("Enter your Name:");
        p.SetName(Console.ReadLine()); 
        p.Print();
        Console.ReadLine();
    }

or if you want to set it on the Deriving class. Do it like this

class Demo : ProtectedDemo
{
    static void Main(string[] args)
    {
        Demo test = new Demo();
        Console.Write("Enter your Name:");
        test.name = Console.ReadLine(); // create an instance of the deriving class, 
                                        // you can only access the name if you're in the current class created
        test.Print();
        Console.ReadLine();
    }
}
DevEstacion
  • 1,947
  • 1
  • 14
  • 28
0

you can only access protected member within the class or with the object on child class(Demo) which inheriting the parent class(ProtectedDemo).

you can access it like.

Demo d = new Demo();
d.name = Console.ReadLine();
Mukund
  • 1,679
  • 1
  • 11
  • 20