473,806 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

override method without extending visibility

Is there any way to have an overridden method which is not callable from the
assembly overridding it? A contrived example (though I want this feature,
my program has nothing to do with food service):

Specifically, how can the DemonstrateCond itions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?

Assembly A
{
public class HealthInspector
{
}

public abstract class LicensedEstabli shment
{
internal abstract void DemonstrateCond itions(HealthIn spector);
}

public static class HealthBureau
{
public static void Complain(Licens edEstablishment restaurant)
{
HealthInspector h = GetResponsibleI nspector();
restaurant.Demo nstrateConditio ns(h);
}
}
}

Assembly B references A
{
public class GoodEats : LicensedEstabli shment
{
// yikes!
}
internal class GoodEatsWaiter {}
}
Nov 6 '06 #1
13 3508

"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:e4******** ******@TK2MSFTN GP04.phx.gbl...
Is there any way to have an overridden method which is not callable from
the assembly overridding it? A contrived example (though I want this
feature, my program has nothing to do with food service):

Specifically, how can the DemonstrateCond itions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
The help for CS0507 suggests I can accomplish this by declaring the base
method "protected internal" and the override as "protected" ... however
CS0507 is still generated.
>
Assembly A
{
public class HealthInspector
{
}

public abstract class LicensedEstabli shment
{
internal abstract void DemonstrateCond itions(HealthIn spector);
}

public static class HealthBureau
{
public static void Complain(Licens edEstablishment restaurant)
{
HealthInspector h = GetResponsibleI nspector();
restaurant.Demo nstrateConditio ns(h);
}
}
}

Assembly B references A
{
public class GoodEats : LicensedEstabli shment
{
// yikes!
}
internal class GoodEatsWaiter {}
}

Nov 6 '06 #2

"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>
"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:e4******** ******@TK2MSFTN GP04.phx.gbl...
>Is there any way to have an overridden method which is not callable from
the assembly overridding it? A contrived example (though I want this
feature, my program has nothing to do with food service):

Specifically , how can the DemonstrateCond itions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter ?

The help for CS0507 suggests I can accomplish this by declaring the base
method "protected internal" and the override as "protected" ... however
CS0507 is still generated.
Filing a bug because the C# compiler clearly does not implement the behavior
indicated by the CS0507 help page.

https://connect.microsoft.com/Visual...dbackID=236091
>
>>
Assembly A
{
public class HealthInspector
{
}

public abstract class LicensedEstabli shment
{
internal abstract void DemonstrateCond itions(HealthIn spector);
}

public static class HealthBureau
{
public static void Complain(Licens edEstablishment restaurant)
{
HealthInspector h = GetResponsibleI nspector();
restaurant.Demo nstrateConditio ns(h);
}
}
}

Assembly B references A
{
public class GoodEats : LicensedEstabli shment
{
// yikes!
}
internal class GoodEatsWaiter {}
}


Nov 6 '06 #3
Ben Voigt wrote:
Filing a bug because the C# compiler clearly does not implement the behavior
indicated by the CS0507 help page.
Before filing a bug report, then why not post some
real code that would give people a fair chance to comment ??

Arne
Nov 7 '06 #4
Hi Ben,

I'm not sure I fully understand your question, so please feel free to let
me know.

Per your question:

==========
Specifically, how can the DemonstrateCond itions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
==========

I think that using "protected internal" on the method should be ok for your
requirement. "protected" means it can only be called by inheriting classes.
"internal" means can only be called by the declaring assembly (in this
case, is assembly A). Combining them means it can be called by inheriting
classes or classes in the same assembly.

When you inherit this class in assemly B, you will notice the overridden
method only has signature "protected" , we're not allowed to use "internal"
here because "internal" to assembly B means it's callable in assembly B
which will change the visibility and violates compiler error CS0507.

So I think the documentation os CS0507 is correct here.

Please let me know if my understanding of your question is correct or not.
Thanks.

Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 7 '06 #5
If I understand your non-code, then your interpretation is incorrect;
"protected internal" allows inherited OR same-assembly access, not AND.

Marking HealthBureau.Co mplain as sealed will prevent the downstream classes
from further overriding, but that doesn't sound like quite what you want...

Marc
Nov 7 '06 #6

"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:l3******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hi Ben,

I'm not sure I fully understand your question, so please feel free to let
me know.

Per your question:

==========
Specifically, how can the DemonstrateCond itions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
==========

I think that using "protected internal" on the method should be ok for
your
requirement. "protected" means it can only be called by inheriting
classes.
"internal" means can only be called by the declaring assembly (in this
case, is assembly A). Combining them means it can be called by inheriting
classes or classes in the same assembly.

When you inherit this class in assemly B, you will notice the overridden
method only has signature "protected" , we're not allowed to use "internal"
here because "internal" to assembly B means it's callable in assembly B
which will change the visibility and violates compiler error CS0507.

So I think the documentation os CS0507 is correct here.
I agree that this interpretation of "protected internal" with a "protected"
override is designed for this situation. However, the compiler (C# 2.0 with
SP1 beta) doesn't actually behave as advertised. CS0507 is generated when
the override is "protected" , and it only compiles when the override is
"protected internal" (accessible from assembly B, NOT what I want).

I've worked around this by making the virtual function protected, and
defining an internal shim method.
>
Please let me know if my understanding of your question is correct or not.
Thanks.

Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 7 '06 #7
However, the compiler (C# 2.0 with
SP1 beta) doesn't actually behave as advertised.
In the definition
(http://msdn2.microsoft.com/en-us/lib...8b(VS.80).aspx) the key
phrase is "defined in referenced metadata" - i.e. separate assemblies.
The "internal" aspect doesn't make it past the assembly edge.

Marc

Nov 8 '06 #8
To clarify - by this I mean that within a single assembly you are bound
by the first condition (changing access), not the second (specifically
relating to protected internal in referenced metadata).

Marc

Nov 8 '06 #9
Hi Ben,

I'm not sure if I understood you correctly, do you mean that having:

virtual protected internal void M() {}

in assembly A, and in assembly B:

protected override internal void M() {}

works while:

protected override void M() {}

doesn't work?
I just verified that on VS2005 and VS2005+SP1 Beta, both indicates only:

protected override void M() {}

works, which is the behavior as documented in compiler error CS0507.

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 8 '06 #10

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

Similar topics

4
4839
by: wooks | last post by:
I have a very simple stylesheet attached to an html file as below. body {color: darkgray; visibility: hidden} span {color: black; visibility: visible} The objective is to only display text within span tags. However when I render the html nothing is visible so it would seem that that the visibility override on the span tags is not taking effect. Can anybody shed light on this?
14
12157
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
4
2222
by: Jazper Manto | last post by:
hi what is the difference between virtual / override and new? until now i only knew the virtual / override thing. thanx for hint. Jazper //--- new ---------------------------------------- public class MyBaseA { public void Invoke() {}
2
4550
by: Flip | last post by:
In java, the default for methods is override. In c# that is not the case. This talks about two classes, the base class and the overriding class. What happens when you have a third class in the mix, extending the second class, how can you indicate the method in the third class is overridding the overridden method? Do you declare the second method to be virtual override? ClassA ------------- virtual DrawWindow(){//code} ^
15
2554
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with 'override' remains a virtual method for further descendent classes." Now here's my question: Let's say you have base class A, and subclasses B and C. Class A contains a virtual method, and B contains an override method. If C didn't have an...
14
7098
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to define some derived classes wich inherit the function and override the variable, but it doesn't work. Testcase:
5
4536
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
4
4276
by: Sean Dockery | last post by:
I am a Java veteran who has just started learning C# with Tom Archer's Inside C# 2nd Ed. In the first chapter, there is a typical Hello, World! application that looks like this... namespace InsideCSharp { class HelloWorldConsoleApp {
5
2233
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the keyword abstract. I have also another class called MyDerivedClass which is a sublass to MyClass. Now to my question when I implement this method myTest in class
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10624
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.