473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Protected Member

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

Feb 15 '07 #1
16 3588

"Fir5tSight" <fi********@yahoo.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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;

?
Feb 15 '07 #2
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

Feb 15 '07 #3
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

Feb 15 '07 #4
Fir5tSight <fi********@yahoo.comwrote:

<snip>
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '07 #5
"Fir5tSight" <fi********@yahoo.coma écrit dans le message de news:
11**********************@m58g2000cwm.googlegroups. com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 15 '07 #6
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

Feb 15 '07 #7
"Fir5tSight" <fi********@yahoo.coma écrit dans le message de news:
11*********************@l53g2000cwa.googlegroups.c om...

|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 [TeamB]
Consultant Software Engineer
Feb 15 '07 #8

"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:ug*************@TK2MSFTNGP02.phx.gbl...
"Fir5tSight" <fi********@yahoo.coma écrit dans le message de news:
11*********************@l53g2000cwa.googlegroups.c om...

|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 [TeamB]
Consultant Software Engineer

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.
Feb 16 '07 #9
Joanna Carter [TeamB] <jo****@not.for.spamwrote:

<snip>
| 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #10
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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 16 '07 #11
"Jon Skeet [C# MVP]" <sk***@pobox.coma écrit dans le message de news:
MP************************@msnews.microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 16 '07 #12
"RobinS" <Ro****@NoSpam.yah.nonea écrit dans le message de news:
sd******************************@comcast.com...

| 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<Customerhas 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<Linelines;

public ReadOnlyCollection<LineLines
{
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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 16 '07 #13
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

Feb 16 '07 #14
Fir5tSight <fi********@yahoo.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #15
"Fir5tSight" <fi********@yahoo.coma écrit dans le message de news:
11**********************@m58g2000cwm.googlegroups. com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 16 '07 #16
Comments below.

"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:es*************@TK2MSFTNGP04.phx.gbl...
"RobinS" <Ro****@NoSpam.yah.nonea écrit dans le message de news:
sd******************************@comcast.com...

| 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<Customerhas 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.
Feb 20 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
3
by: Jordan Taylor | last post by:
I am confused about protected member functions and objects. Is there any particular advantage of declaring members protected?
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
2
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.