Protected Member

  • Thread starter Thread starter Fir5tSight
  • Start date Start date
F

Fir5tSight

Hi All,

I have a small C#.NET program that is as follows:

using System;

class A
{
protected int x = 123;
}

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

a.x = 10; // Compiling error here
b.x = 10;
}

}

Now I get a compiling error:
Cannot access protected member 'A.x' via a qualifier of type 'A'; the
qualifier must be of type 'B' (or derived from it)

I'm confused - Since "x" is defined in class "A", how come "a" which
is of class "A" cannot access "x" while "b" which is not of class "A"
can?

Thanks,
-Emily
 
Fir5tSight said:
Hi All,

I have a small C#.NET program that is as follows:

using System;

class A
{
protected int x = 123;
}

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

a.x = 10; // Compiling error here
b.x = 10;
}

}

Now I get a compiling error:
Cannot access protected member 'A.x' via a qualifier of type 'A'; the
qualifier must be of type 'B' (or derived from it)

I'm confused - Since "x" is defined in class "A", how come "a" which
is of class "A" cannot access "x" while "b" which is not of class "A"
can?

Thanks,
-Emily
Are you sure that you're not getting the error because you're attempting to
initialize a field in a class definition?
I.e. where you:

protected int x = 123;

?
What happens if you just

protected int x;

?
 
I am getting the error. I have to comment out the line of code:

//a.x = 10; // Compiling error here

in order for it to compile.

-Emily
 
I am getting the error. In order for it to compile successfully, I
must comment out the line of code:

//a.x = 10; // Compiling error here

My question is: Why does this line of code cause compiling error?

-Emily
 
Now I get a compiling error:
Cannot access protected member 'A.x' via a qualifier of type 'A'; the
qualifier must be of type 'B' (or derived from it)

I'm confused - Since "x" is defined in class "A", how come "a" which
is of class "A" cannot access "x" while "b" which is not of class "A"
can?

You only get access to the protected members of instances of your own
class, not to just any instance of the base class.

The C# spec puts it somewhat cryptically:

<quote>
When a protected instance member is accessed outside the program text
of the class in which it is declared, and when a protected internal
instance member is accessed outside the program text of the program in
which it is declared, the access is required to take place through an
instance of the derived class type in which the access occurs.
</quote>

Effectively, you're allowed to access things you've inherited yourself,
but you can't start stealing someone else's inheritance :)
 
"Fir5tSight" <[email protected]> a écrit dans le message de (e-mail address removed)...

| class A
| {
| protected int x = 123;
| }
|
| class B : A
| {
| static void Main()
| {
| A a = new A();
| B b = new B();
|
| a.x = 10; // Compiling error here
| b.x = 10;
| }
|
| }
|
| Now I get a compiling error:
| Cannot access protected member 'A.x' via a qualifier of type 'A'; the
| qualifier must be of type 'B' (or derived from it)
|
| I'm confused - Since "x" is defined in class "A", how come "a" which
| is of class "A" cannot access "x" while "b" which is not of class "A"
| can?

First question: why are you holding an instance of A inside B ? An instance
of B *is* an instance of A already.

As others have said, even though B derives from A, protected members of A
are only available from *the same instance* of a derived class.

Joanna
 
I don't know why one would hold an instance of A inside B. This is
sample code from Microsoft training class illustrating the use of
protected member.

It says b.x is valid while a.x is not valid and that's why I get
compiling error at a.x = 10;

Sorry, after all of you have tried to explain this to me, I still
don't understand. Why a.x is not valid, but b.x is valid?

I see that x is defined in A of which a is an instance.

-Emily
 
"Fir5tSight" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I don't know why one would hold an instance of A inside B. This is
| sample code from Microsoft training class illustrating the use of
| protected member.

In that case, the code is purely to demonstrate that it is only possible to
access protected members from the same instance of a given derived class as
opposed to from an instance of the derived class that contains an instance
of A.

| It says b.x is valid while a.x is not valid and that's why I get
| compiling error at a.x = 10;

And that is correct. a.x is not visible from an instance of B that
*contains* an instance of A.

| Sorry, after all of you have tried to explain this to me, I still
| don't understand. Why a.x is not valid, but b.x is valid?
|
| I see that x is defined in A of which a is an instance.

Correct again, but there is a difference which I shall try to explain inline
with your code.

class A
{
protected int x = 123;
}

x is s protected field in the class A. This means that any code inside the A
class can access x, as can any code inside a class derived from A.

class Test
{
static void Main()
{
A a = new A();

a.x = 10; // will not compile
}
}

This will fail because x is only visible inside instances of A or its
derivatives. Test does not derive from A, therefore it can't see x.

class B : A
{
public void AssignX()
{
x = 10; // this compiles
}
}

This will work because B derives from A and can see the protected field x.

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

b.x = 10; // this compiles
}
}

This works because code inside B can see anything inside any instance of B,
even the private and protected members like x.

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

a.x = 10; // will not compile
}
}

This, however, shows that although B derives from A, code inside the B class
cannot see private and protected members of A, because 'a' is not an
instance of B and therefore does not allow access to its private and
protected members.

Code inside derived classes can only see private and protected members of
other instances of that derived class, not instances of the base class. the
privilege of seeing members of the base class is only accorded to code
inside *the same instance* of the *derived* class.

Finally, you should note that the Main() method is marked as static and
therefore could not see *any* instance fields, only static fields. This is
why the example shows creating instances of A and B.

Does that help ?

Joanna
 
Joanna Carter said:
"Fir5tSight" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I don't know why one would hold an instance of A inside B. This is
| sample code from Microsoft training class illustrating the use of
| protected member.

In that case, the code is purely to demonstrate that it is only possible
to
access protected members from the same instance of a given derived class
as
opposed to from an instance of the derived class that contains an
instance
of A.

| It says b.x is valid while a.x is not valid and that's why I get
| compiling error at a.x = 10;

And that is correct. a.x is not visible from an instance of B that
*contains* an instance of A.

| Sorry, after all of you have tried to explain this to me, I still
| don't understand. Why a.x is not valid, but b.x is valid?
|
| I see that x is defined in A of which a is an instance.

Correct again, but there is a difference which I shall try to explain
inline
with your code.

class A
{
protected int x = 123;
}

x is s protected field in the class A. This means that any code inside
the A
class can access x, as can any code inside a class derived from A.

class Test
{
static void Main()
{
A a = new A();

a.x = 10; // will not compile
}
}

This will fail because x is only visible inside instances of A or its
derivatives. Test does not derive from A, therefore it can't see x.

class B : A
{
public void AssignX()
{
x = 10; // this compiles
}
}

This will work because B derives from A and can see the protected field
x.

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

b.x = 10; // this compiles
}
}

This works because code inside B can see anything inside any instance of
B,
even the private and protected members like x.

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

a.x = 10; // will not compile
}
}

This, however, shows that although B derives from A, code inside the B
class
cannot see private and protected members of A, because 'a' is not an
instance of B and therefore does not allow access to its private and
protected members.

Code inside derived classes can only see private and protected members of
other instances of that derived class, not instances of the base class.
the
privilege of seeing members of the base class is only accorded to code
inside *the same instance* of the *derived* class.

Finally, you should note that the Main() method is marked as static and
therefore could not see *any* instance fields, only static fields. This
is
why the example shows creating instances of A and B.

Does that help ?

Joanna

So say I had a class called Customer that had a protected property called
PhoneNumber.

Then I had a class called CustomerList that was a List(Of Customer), I
could see the phone number, right? Because each item in the list is a
Customer?

But if i had a class called Product that instantiated a Customer inside it,
I could not see the PhoneNumber.

Is that right?

Robin S.
 
| I'm confused - Since "x" is defined in class "A", how come "a" which
| is of class "A" cannot access "x" while "b" which is not of class "A"
| can?

First question: why are you holding an instance of A inside B ? An instance
of B *is* an instance of A already.

As others have said, even though B derives from A, protected members of A
are only available from *the same instance* of a derived class.

No, that's not quite right. Code in B can access protected members of A
"through" any instance of B or a derived class. It doesn't have to be
any particular instance of B, and in particular it doesn't need to be
"this" during an instance method.

The important thing is that you're accessing the member through an
object known (at compile time) to be an instance of B, not just an
instance of A or some different derived class.
 
Emily

Protected members of a class are not accessible to instances of the
class. They are *protected*, which means that they are accessible only
through inheritance and since your B class has inherited from A, it
inherits the protected members as well.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
"RobinS" <[email protected]> a écrit dans le message de (e-mail address removed)...

| So say I had a class called Customer that had a protected property called
| PhoneNumber.

Which is very doubtful because then it would not be visible to other
classes.

| Then I had a class called CustomerList that was a List(Of Customer), I
| could see the phone number, right? Because each item in the list is a
| Customer?

No, this is not correct. List<Customer> has no inheritance relationship with
Customer, it is simply a list that happens to hold Customer instances.

| But if i had a class called Product that instantiated a Customer inside
it,
| I could not see the PhoneNumber.

This is correct, but I feel that you really don't quite understand what is
involved here.

There are four visibilities that are commonly used in class design: private,
protected, internal and public.

Private visibility means that a member of a class can only ever be accessed
from code within that class.

Protected visibility means that a member of a class so marked can only ever
be accessed from code within either that class or from the code within any
class that derives or inherits from that class.

Internal visibility means that a member of a class can be accessed by code
in any class that is included in the same assembly as the declaring class.

Public visibility means that a member of a class can be accessed by any code
in any class anywhere in any assembly or program.

class Order
{
class Line
{
...
internal Line()
{
...
}
}

private List<Line> lines;

public ReadOnlyCollection<Line> Lines
{
get { return new ReadOnlyCollection<Line>(lines) }
}

public OrderLine AddLine()
{
Line newLine = new Line();
lines.Add(newLine);
return newLine;
}
}

class Test
{
static void Main()
{
Order order = new Order();
Order.Line line = order.AddLine();
line. ......
}
}

In this simple example, I want to hold a list of OrderLines in an Order but,
I don't want any other code outside of the Order class to be able to modify
that list. To this end, I declare the list in a private field but add a
public property that returns, not the original list, but a readonly list
wrapper that allows outside code to look at and modify items in the list but
that prohibits modification of the list itself.

The Order.Line class is nested in the Order class, thus allowing us indicate
to users that Line was part of the concept of an Order. Note that the
constructor of Line needs to be marked in such a way that only the Order
class is allowed to create instances of the Line class.

The constructor of the Line class cannot be marked as private or even
protected because, even though the Line class is part of the Order class,
its members' visibilities are regarded as if those members were in a totally
separate class. Therefore, in order to allow the Line constructor to be
visible to the Order class, we have to mark it as internal, as this is the
only way to allow another class in the same assembly to instantiate the Line
class.

class HelperClass
{
public void DoSomething()
{
...
}
}

class BaseClass
{
private HelperClass helper = new HelperClass();

protected HelperClass Helper
{
get { return helper; }
}
}

class DerivedClass : BaseClass
{
public void UseHelper()
{
Helper.DoSomething();
}
}

This is a typical use of the protected visibility to hide "helper" behaviour
from all classes except the class being helped.

I think your confusion arises because the original example that you quoted
tries to call a protected member on an instance of a base class from within
a method of a derived class.

In the same way, as I would not able to see a protected constructor on the
Line class, even though it was inside the Order class, so I cannot see
protected members of *any* class, unless the accessing code is part of the
declaring class or its derivatives.

class BaseClass
{
protected int x;
}

class DerivedClass : BaseClass
{
void Test()
{
BaseClass b = new BaseClass();

b.x // not visible

BaseClass b = new DerivedClass();

((DerivedClass) b).x = 10; // visible

In this second example, because b is really an instance of DerivedClass held
in a BaseClass reference, it can then be cast to DerivedClass and then the
protected member x is accessible due to the fact that we are in the code of
the DerivedClass.

BaseClass b = new BaseClass();

b.x // not visible

This Test() method is part of DerivedClass and that allows it to see any
protected members of BaseClass, but only if those members are actually part
of an instance of DerivedClass. In this last example, b is not DerivedClass,
it is only BaseClass, therefore x is not visible to *any* code outside of
BaseClass.

Just because DerivedClass inherits from BaseClass, it doesn't mean that code
inside DerivedClass can see protected members of BaseClass. This is only
allowed if the protected member is in the *same instance* of DerivedClass.

Any better ? Or just more confusing ?

Do you understand the difference between a class and an object or instance
of that class ?

Joanna
 
"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de (e-mail address removed)...

| No, that's not quite right. Code in B can access protected members of A
| "through" any instance of B or a derived class. It doesn't have to be
| any particular instance of B, and in particular it doesn't need to be
| "this" during an instance method.
|
| The important thing is that you're accessing the member through an
| object known (at compile time) to be an instance of B, not just an
| instance of A or some different derived class.

Thanks Jon, much more succinct, and more importantly correct :-)

Joanna
 
Thanks J.V.Ravichandran!

Now I understand: Next time I see a protected member in a class, and
if I want to access the member, I must define a child class and access
that member via an instance of the child class.

This is because I won't be able to access the protected member via an
instance of the class in which the protected member is initially
defined.

-Emily
 
Fir5tSight said:
Thanks J.V.Ravichandran!

Now I understand: Next time I see a protected member in a class, and
if I want to access the member, I must define a child class and access
that member via an instance of the child class.

This is because I won't be able to access the protected member via an
instance of the class in which the protected member is initially
defined.

If you ever create a derived class purely to access a protected member,
that's a sign that you're doing something wrong. Usually a protected
member is made available so that a class which *naturally* derives from
the base class can do something. Personally it's not a modifier I use
often anyway, but "getting round it" by deriving just for the sake of
it is a bad code smell.
 
"Fir5tSight" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Now I understand: Next time I see a protected member in a class, and
| if I want to access the member, I must define a child class and access
| that member via an instance of the child class.

No, no, no!! This is not what protected members are for. Theay are
deliberately designed not to be accessible from outside of either the
declaring class or a derived class.

| This is because I won't be able to access the protected member via an
| instance of the class in which the protected member is initially
| defined.

I think you do not yet understand the difference between a class and an
instance; would that be true ?

Joanna
 
Comments below.

Joanna Carter said:
"RobinS" <[email protected]> a écrit dans le message de (e-mail address removed)...

| So say I had a class called Customer that had a protected property
called
| PhoneNumber.

Which is very doubtful because then it would not be visible to other
classes.

*** Right, I was just trying to say it with field names that made sense to
me. It would be stupid to do this to a PhoneNumber.
| Then I had a class called CustomerList that was a List(Of Customer), I
| could see the phone number, right? Because each item in the list is a
| Customer?

No, this is not correct. List<Customer> has no inheritance relationship
with
Customer, it is simply a list that happens to hold Customer instances.

*** What I meant was if I took an entry from the list and cast it as
Customer, I could see the phone number, right? Like here:

For each cust as Customer In myCustomerList
Debug.Print(cust.PhoneNumber)
next cust
| But if i had a class called Product that instantiated a Customer inside
it,
| I could not see the PhoneNumber.

This is correct, but I feel that you really don't quite understand what
is
involved here.

***That was the point I was trying to make sure I understood, was the
mechanics of it.

The rest of it I'm going to have to think about. I thought I understood
this, but now I'm confused. I'll post back in a couple of days if I can't
figure it out.

Robin S.
 
Back
Top