accessing proteceted member field

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Why is it that in the example below I get an error saying that I
"cannot access protected member 'i' via a qualifier of type 'Base';
the qualifier must be of type 'Derived'"?? but when i make m_i public
it works fine and i can access the member.

class Base
{
protected int m_i; //doesn't work
//public int m_i; //works

public Base()
{
m_i = 0;
}
}

class Derived:Base
{
public void test(Base b)
{
int test = b.m_i;
}
}
 
I pulled this straight out of the Visual Studio .NET Help
Documentation (type the following in the search
panel "Compiler Error CS1540"):

"Although a derived class can access protected members of
its base class, it cannot do so through an instance of the
base class."

In short, the "m_i" you're trying to retrieve is from
the "b" instance of Base class and not the "m_i" of the
derived class.

-Fabricio
 
Back
Top