472,119 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

inconsistent accessibility error

Consider the following code fragment

public class Wrapper {
protected enum E { IN, OUT };
public class C {
protected void foo(E e) { }
}
}

I want the class C to be accessible from outside the wrapper class, but not
the method foo. Compiling this gives the error:

Inconsistent accessibility: parameter type Wrapper.E' is less accessible
than method Wrapper.C.foo

but the accessibility of both types is "protected"
Nov 25 '05 #1
5 27157
C can't see E simply because E does not subclass C - it is *embedded* in it;
protected items are only available to sub-classes.

Possible fixes:
Move E to inside C
Make E public - it is only an enum after all
(possibly something involving making E internal protected and C internal,
but that has it's own limitations)

Hope that helps,

Marc

"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Consider the following code fragment

public class Wrapper {
protected enum E { IN, OUT };
public class C {
protected void foo(E e) { }
}
}

I want the class C to be accessible from outside the wrapper class, but
not the method foo. Compiling this gives the error:

Inconsistent accessibility: parameter type Wrapper.E' is less accessible
than method Wrapper.C.foo

but the accessibility of both types is "protected"

Nov 25 '05 #2
Sorry - I mean't:
C can't see E simply because C does not subclass
Wrapper, and E is defined (protected) in Wrapper

Marc

"Marc Gravell" <mg******@rm.com> wrote in message
news:e3**************@TK2MSFTNGP14.phx.gbl...
C can't see E simply because E does not subclass C - it is *embedded* in
it; protected items are only available to sub-classes.

Possible fixes:
Move E to inside C
Make E public - it is only an enum after all
(possibly something involving making E internal protected and C internal,
but that has it's own limitations)

Hope that helps,

Marc

"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Consider the following code fragment

public class Wrapper {
protected enum E { IN, OUT };
public class C {
protected void foo(E e) { }
}
}

I want the class C to be accessible from outside the wrapper class, but
not the method foo. Compiling this gives the error:

Inconsistent accessibility: parameter type Wrapper.E' is less accessible
than method Wrapper.C.foo

but the accessibility of both types is "protected"


Nov 25 '05 #3
Andy Fish <aj****@blueyonder.co.uk> wrote:
Consider the following code fragment

public class Wrapper {
protected enum E { IN, OUT };
public class C {
protected void foo(E e) { }
}
}

I want the class C to be accessible from outside the wrapper class, but not
the method foo. Compiling this gives the error:

Inconsistent accessibility: parameter type Wrapper.E' is less accessible
than method Wrapper.C.foo

but the accessibility of both types is "protected"


Consider the following:

public class Subclass extends C
{
public void Something()
{
foo(????);
}
}

foo is accessible here, but E isn't. Hence the error.

Does foo definitely need to be protected rather than private?

--
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
Nov 25 '05 #4
Marc Gravell <mg******@rm.com> wrote:
Sorry - I mean't:
C can't see E simply because C does not subclass
Wrapper, and E is defined (protected) in Wrapper


No, C can definitely see E due to being nested within T. See section
10.5.2 of the spec (ECMA numbering):

<quote>
The accessibility domain of a nested member M declared in a type T
within a program P, is defined as follows (noting that M itself may
possibly be a type):

[...]
If the declared accessibility of M is protected, let D be the union of
the program text of T and the program text of any type derived from T.
</quote>

Now, in our case, C is within the program text of Wrapper, so it has
access to E.

The problem is that things which may logically be able to call foo (by
deriving from C) wouldn't have access to E.

--
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
Nov 25 '05 #5
Fair enough! Learn something new every day...

;-p

Marc

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Marc Gravell <mg******@rm.com> wrote:
Sorry - I mean't:
C can't see E simply because C does not subclass
Wrapper, and E is defined (protected) in Wrapper


No, C can definitely see E due to being nested within T. See section
10.5.2 of the spec (ECMA numbering):

<quote>
The accessibility domain of a nested member M declared in a type T
within a program P, is defined as follows (noting that M itself may
possibly be a type):

[...]
If the declared accessibility of M is protected, let D be the union of
the program text of T and the program text of any type derived from T.
</quote>

Now, in our case, C is within the program text of Wrapper, so it has
access to E.

The problem is that things which may logically be able to call foo (by
deriving from C) wouldn't have access to E.

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

Nov 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steven Quail | last post: by
1 post views Thread by Sunit Joshi | last post: by
3 posts views Thread by AAV | last post: by

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.