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.
Read following blog post by Eric Lippert,
try to use
protected internalit will work