473,398 Members | 2,525 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,398 software developers and data experts.

Calling protected members

Hi there

The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale?
Are there plans to change this? This feature forces me to make things public when the operations are in fact protected. This breaks a lot of patterns for me

public class C
public void F( C other)
this.G()
other.G(); // **

protected void G(
{
Thanks you
Tom.
Nov 16 '05 #1
7 7240
TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
0SPA|/\|t*******@hotmail.com|/\|APS0_N_>> wrote:
The line marked *** doesn't compile.


Yes it does. At least, it does for me. Could you give a short but
complete example which *doesn't* compile. This is what I tried and
managed to compile:

public class C {
public void F( C other) {
this.G();
other.G(); // ***
}
protected void G()
{}
}

// Just to avoid having to compile it to a library
class Test
{
static void Main()
{
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Jon

I tried to strip my problem in a minimal way, and I guess it was just too minimal. The problem is more complex. Second try

public class Base
protected abstract void DoStuff()
public class Derived_1 : Base
protected override void DoStuff() {
public class Derived_2 : Base
private Derived_1[] pool

protected override void DoStuff()
foreach( Base base in pool
base.DoStuff(); // **

The error message when compiling is
<error_message
"C:\Blabla\Derived_2.cs(94): Cannot access protected member Base.DoStuff()' via a qualifier of type 'Base'; the qualifier must be of type 'Derived_2' (or derived from it)
</error_message

The problem arises in my thread pool class. A thread pool (Derived_2) manages several threads (Derived_1), and is a thread itself (Base). The thread pool thread dispatches tasks to idle pool threads. In my project I cannot make DoStuff public, because it is implementation functionality, not public functionality

This makes a lot of patterns impossible to implement elegantly. It forces me to make DoStuff public, whereas the operation is really protected. Internal would work only within the same namespace, and that isn't sufficient in my case (extensible library)

What do you think
PS: I know that there is already a thread pool class in C#. I just wanted my own

Thanks
Tom

----- Jon Skeet [C# MVP] wrote: ----

TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N
0SPA|/\|t*******@hotmail.com|/\|APS0_N_>> wrote
The line marked *** doesn't compile.


Yes it does. At least, it does for me. Could you give a short but
complete example which *doesn't* compile. This is what I tried and
managed to compile

public class C
public void F( C other)
this.G()
other.G(); // **

protected void G(
{
// Just to avoid having to compile it to a librar
class Tes

static void Main(


--
Jon Skeet - <sk***@pobox.com
http://www.pobox.com/~skee
If replying to the group, please do not mail me to

Nov 16 '05 #3
Jon

Actually I think it is logical behaviour. But I don't see a good way of working around it, except making the DoStuff method internal and place the thread class, thread pool class and task executer class in the same namespace

Tom.
Nov 16 '05 #4
=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?= wrote:
public class Base {
protected abstract void DoStuff();
}

public class Derived_2 : Base {
protected override void DoStuff() {
foreach( Base base in pool )
base.DoStuff(); // ***
}
}


You cannot call an abstract method!

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
Nov 16 '05 #5
TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
0SPA|/\|t*******@hotmail.com|/\|APS0_N_>> wrote:
I tried to strip my problem in a minimal way, and I guess it was just
too minimal. The problem is more complex. Second try:
<snip>
The error message when compiling is:
<error_message>
"C:\Blabla\Derived_2.cs(94): Cannot access protected member
Base.DoStuff()' via a qualifier of type 'Base'; the qualifier must be
of type 'Derived_2' (or derived from it)"
</error_message>
Right. Basically, you can call protected methods which have been
declared by a parent on *this* (or another instance of the same class),
and you can call protected methods which have been declared by a
descendant of your current class, but you can't call protected methods
declared by a parent on an instance of a different class.

This is (I believe) to stop you from calling protected methods
"across" the inheritance hierarchy, when they're designed to be called
*within* the inheritance hierarchy (up or down).

See http://www.jaggersoft.com/csharp_standard/10.5.3.htm for more
information.
The problem arises in my thread pool class. A thread pool (Derived_2)
manages several threads (Derived_1), and is a thread itself (Base).
The thread pool thread dispatches tasks to idle pool threads. In my
project I cannot make DoStuff public, because it is implementation
functionality, not public functionality.

This makes a lot of patterns impossible to implement elegantly. It
forces me to make DoStuff public, whereas the operation is really
protected. Internal would work only within the same namespace, and
that isn't sufficient in my case (extensible library).


Namespaces have nothing to do with accessibility - internal would work
within the same *assembly*.

I had a bit of trouble following your example, to be honest, but I
think you're really trying to bend protected access to do something
it's not designed for in .NET.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jochen

That is not true. I can always call a method declared abstract on a reference variable. I just can't create an object from an abstract class. Two different issues

Base b = new Base(); // impossible, abstract clas

Base d = new Derived_1(); // no probs, Derived_1 not abstrac
d.DoStuff(); // no prob

This is equivalent in the foreach loop of my example, the Derived_1 references are cast to Base references

Tom

----- Jochen Kalmbach wrote: ----

=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?= wrote
public class Base
protected abstract void DoStuff()
public class Derived_2 : Base

protected override void DoStuff()
foreach( Base base in pool
base.DoStuff(); // **


You cannot call an abstract method

--
Greeting
Joche

Do you need a memory-leak finder
http://www.codeproject.com/tools/leakfinder.as
Do you need daily reports from your server
http://sourceforge.net/projects/srvreport

Nov 16 '05 #7
Jon

----- Jon Skeet [C# MVP] wrote: ----

TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N
0SPA|/\|t*******@hotmail.com|/\|APS0_N_>> wrote
[...]
The error message when compiling is
<error_message>> "C:\Blabla\Derived_2.cs(94): Cannot access protected membe
Base.DoStuff()' via a qualifier of type 'Base'; the qualifier must b
of type 'Derived_2' (or derived from it)
</error_message
Right. Basically, you can call protected methods which have been
declared by a parent on *this* (or another instance of the same class),
and you can call protected methods which have been declared by a
descendant of your current class, but you can't call protected methods
declared by a parent on an instance of a different class

This is (I believe) to stop you from calling protected methods
"across" the inheritance hierarchy, when they're designed to be called
*within* the inheritance hierarchy (up or down)

See http://www.jaggersoft.com/csharp_standard/10.5.3.htm for more
information

I see. It is actually very logical..

[... This makes a lot of patterns impossible to implement elegantly. I
forces me to make DoStuff public, whereas the operation is reall
protected. Internal would work only within the same namespace, an
that isn't sufficient in my case (extensible library)


Namespaces have nothing to do with accessibility - internal would work
within the same *assembly*

Thanks for the correction

I had a bit of trouble following your example, to be honest, but I
think you're really trying to bend protected access to do something
it's not designed for in .NET
--
Jon Skeet - <sk***@pobox.com>

Indeed. I will have to either use internal, or make public helpers, or redesign ;-

Thanks
Tom

Nov 16 '05 #8

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. ...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
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...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
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...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.