473,395 Members | 1,653 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,395 software developers and data experts.

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.MyInteralClass' is less accessible than field
'MyNamespace.MyPublicClass.MyField'

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 6037
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 "Accessibility 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
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...
3
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...
10
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;...
7
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...
4
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...
3
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...
4
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
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...
9
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.