473,763 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using an internal class as type of a field marked protected intern

Hi everybody,

The C# compiler reports a Compiler Error CS0052 in the following situation:

I declared a type marked as "internal":

namespace MyNamespace
{

internal class MyInteralClass
{
// ...
}

/*
I tried to use this type in a public class, and I want it to be accessible
only for devired classes in the same assembly so I set its accessibility
level to protected internal.
*/

public class MyPublicClass
{
protected internal MyInteralClass MyField;

// ...
}

}

I tried to compile the source and the compiler reports the CS0052 error:

error CS0052: Inconsistent accessibility: field type
'MyNamespace.My InteralClass' is less accessible than field
'MyNamespace.My PublicClass.MyF ield'

Is it a bug? Cannot such consrtuction be used? I didn't find any details for
this situation in the MDSN Library.

Thanks a lot, best
Áron Kolozs
Nov 17 '05 #1
2 6061
It's not a bug. The error message is accurate.

The problem is that you are misinterpreting what "protected internal"
means. I gather from the way you phrased your post that you believe
that the two keywords have an "and" relation: that is, "protected"
further restricts "internal" so that the field is available only to
child classes within the same assembly. This is not what "protected
internal" means.

"protected internal" means that the field is available to _all_ classes
within the same assembly, _and_ any child classes, whether they are
within the same assembly or not. So, some class _outside_ your assembly
could declare itself:

public class SomeOtherClass : MyPublicClass

and it would have access to "MyField". "protected internal" is _less_
restrictive than either "protected" or "internal" alone: in effect, the
two access modifiers have an "or" relation, not an "and" relation.

So, in my example above, SomeOtherClass would have access to MyField
(because it's protected internal, and SomeOtherClass is a child class
of MyPublicClass even though it's in a different assembly), but would
not have access to MyInternalClass (because it's declared as strictly
internal to your assembly, and SomeOtherClass is in a different
assembly). Therefore, the access modifiers are inconsistent.

Change MyField to be "internal" only, and the message will go away. You
might as well, since there is no access modifier in C# to do what you
want (only child classes within the same assembly).

Nov 17 '05 #2
First of all, thank a lot for clearing up. I have dropped the "protected"
declaration from the front of the "interal" and then the source was compiled
successfully.

You're right, I was confused, because the "or" is escaped my attention. I
read the "Accessibil ity levels" chapter again and according to your
interpretation I realised the "or".

On reflection, we have to admit that the "protected" _and_ "internal"
declaration has no reason, because a developer has to use their own classes
correctly and no need for self restriction and other mazohism :)

Best,
Kolozs Áron
Nov 17 '05 #3

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

Similar topics

0
1624
by: Jurrie | last post by:
Hi all, I have a XML-document which i want to transform into PDF with apaches FOP. In my XML document i have <href> tags that are links to either a chapter in the document or to a place on the www. example: <ref type="intern" to="chapter 2"/> and <ref type="extern" to="http://www.website.com"/> In my XSL document i have this:
3
5080
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member through an interface reference. I have tried to change the snd argement to SetAttribute method from 'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
10
4162
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName; string s_FinalFileName; try
7
9822
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class within the same program/assembly. The two classes are not 'related' (inherited). How can I do this. I cant see how the keyword Internal can be used for this purpose.
4
2804
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than one runat=server form on a asp.net page. However, I still want developers to be able to use asp.net controls to create some apps that are created on the page. So I need multiple forms on a asp.net page(doesn't everyone). I purchased the...
3
2158
by: ssg31415926 | last post by:
I have an abstract base class from which has concrete subclasses. I have a method on the base class marked as 'protected internal'. I have another class in the same assembly which holds a reference to an instance of one subclass. I don't seem to be able to see the protected internal method. Should I? If not, why not? (If I add a protected internal method to the subclass, I can access it.)
4
6303
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
13
2826
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes to another assembly, the compiler complains that the override in the derived class, also declared as 'protected internal', is trying to change the access modified from 'protected', which is clearly not the case. (I have checked the metadata in...
9
5172
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier for these classes. I have public classes that use the internal classes, but I get a CS0051 error if I use an internal class as a parameter to a protected function of a public class. For example: internal class InternalClass { }
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9386
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
10145
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...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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...
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.