-1

See the question in code:

class commonParent {
     protected string name;
}

class child1:commonParent{
    // do some stuff
}

class child2:commonParent{
   // do some stuff

   protected void test(){
       child1 myChild1 = new child1();

       //is it possible to access myChild1.name in child2 without 
       //declaring the name public or internal?

      // I want to do something like this:
      string oldName = myChild1.name;

      //but I got the error:
      //Error   46  Cannot access protected member 'commonParent.name' 
      //via a qualifier of type 'child1'; the qualifier must be of 
      //type 'child2' (or derived from it)  
   }
}

The field "name" is only used by all children of commonParent class. I want to hide this field from outside (classes not derived from commonParent) while leaving it accessible within the scope of commonParent and its children.

LazNiko
  • 2,083
  • 3
  • 25
  • 39
  • 1
    Did it give any error when you tried to access myChild1.name? – Sachin Shanbhag Oct 15 '11 at 07:57
  • Yes, I gor this error: Error 46 Cannot access protected member 'commonParent.name' via a qualifier of type 'child1'; the qualifier must be of type 'child2' (or derived from it) – LazNiko Oct 15 '11 at 08:02

5 Answers5

1

Read following blog post by Eric Lippert,

try to use protected internal it will work

Damith
  • 62,401
  • 13
  • 102
  • 153
  • nice article, though it doesn't give any answer. it seems "protected internal" is lowest visibility i can set to get things done. – LazNiko Oct 15 '11 at 08:41
0

My approach to this is to provide a protected static method that does have access to the protected value, but is only available to the derived classes, like in the code below:

class commonParent
{
    protected string name;

    protected static string GetName(commonParent other)
    {
        return other.name;
    }
}

class child1 : commonParent
{
    // do some stuff
}

class child2 : commonParent
{
    protected void test()
    {
        child1 myChild1 = new child1();
        string oldName = commonParent.GetName(myChild1);

    }
}

This has the benefit of providing access to protected data only to derived classes.

Michael Erickson
  • 4,217
  • 2
  • 23
  • 10
0

you have to declare it at least protected

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

I think this is bad design ,you should declare Name as Private and create a property with Get,Set accessors ,where you can chose if Get or Set could be Public ,Private or Protected ,otherwise Protected will allow any classes of the Same namespace to access a Field or Property .

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
  • yeah I know that's the rule. but in my situation, the field "name" is only used by all children of commonParent class. I want to hide this field from outside but leaving it accessible within the scope of commonParent and its children. – LazNiko Oct 15 '11 at 08:08
0

To answer your question, you can access them using reflection. This is not something you want to rely on, though.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72