Connecting Tech Pros Worldwide Forums | Help | Site Map

inconsistent accessibility error

Andy Fish
Guest
 
Posts: n/a
#1: Nov 25 '05
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"



Marc Gravell
Guest
 
Posts: n/a
#2: Nov 25 '05

re: inconsistent accessibility error


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" <ajfish@blueyonder.co.uk> wrote in message
news:ONe1jib8FHA.1280@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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"
>
>[/color]


Marc Gravell
Guest
 
Posts: n/a
#3: Nov 25 '05

re: inconsistent accessibility error


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" <mgravell@rm.com> wrote in message
news:e3n0nvb8FHA.3484@TK2MSFTNGP14.phx.gbl...[color=blue]
>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" <ajfish@blueyonder.co.uk> wrote in message
> news:ONe1jib8FHA.1280@TK2MSFTNGP10.phx.gbl...[color=green]
>> 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"
>>
>>[/color]
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 25 '05

re: inconsistent accessibility error


Andy Fish <ajfish@blueyonder.co.uk> wrote:[color=blue]
> 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"[/color]

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 - <skeet@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
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 25 '05

re: inconsistent accessibility error


Marc Gravell <mgravell@rm.com> wrote:[color=blue]
> Sorry - I mean't:
> C can't see E simply because C does not subclass
> Wrapper, and E is defined (protected) in Wrapper[/color]

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 - <skeet@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
Marc Gravell
Guest
 
Posts: n/a
#6: Nov 28 '05

re: inconsistent accessibility error


Fair enough! Learn something new every day...

;-p

Marc

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1df17576aaaed65d98caa4@msnews.microsoft.c om...[color=blue]
> Marc Gravell <mgravell@rm.com> wrote:[color=green]
>> Sorry - I mean't:
>> C can't see E simply because C does not subclass
>> Wrapper, and E is defined (protected) in Wrapper[/color]
>
> 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 - <skeet@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[/color]


Closed Thread