1

I have the following code in C# (.Net)

public class A
{
    protected internal enum Color {WHITE, BLACK};
}

public class B
{
    protected internal int methodOne(A.Color color)
    {
        if (color == A.Color.WHITE)
            return 1;
        return 2;
    }
}

But I'm getting the following error: error CS0051: Inconsistent accessibility: parameter type 'A.Color' is less accessible than method 'B.methodOne(A.Color)'

Why does it say the parameter is "less accessible" when both the parameter type and the method have the same access modifier (protected internal)?

Update #1: this is what I really want to achieve:

public class A
{
    internal enum Color {WHITE, BLACK};
}

public class B
{
    protected int methodOne(A.Color color)
    {
        if (color == A.Color.WHITE)
            return 1;
        return 2;
    }
}

public class C : B
{
    int methodTwo()
    {
        return methodOne(A.Color.WHITE);
    }
}

I want to keep enum Color as internal, and I want methodOne to be accessible only by derived classes. Is this possible?

abe
  • 11
  • 2
  • You probably want to derive class B from class A if you want to use protected in a useful way. – user8276908 May 07 '20 at 00:49
  • What I understand is that protected makes the member accessible only by the same class or derived classes, which in this case they aren't. But wouldn't internal make it accessible if both classes are in the same assembly? If I remove 'protected' from both of them it works, but I want methodOne to be accessed by another class that derives form B (C : B) – abe May 07 '20 at 00:51
  • 1
    Because `B` is `public`, it can be used by other assemblies, which wouldn't be able to access `Color` because it's `protected` within `A` and `internal` to that assembly. – madreflection May 07 '20 at 00:52
  • `But wouldn't internal make it accessible if both classes are in the same assembly?` Yes it would. But the compiler doesn't know that is what you are going to do. As an example - let's say you created a new class C that inherited from B. C lives in a different assembly. Now, C has access to `methodOne` (since it is `protected`). But it **doesn't** have access to `Color` (since it is in a different assembly, and doesn't inherit from `A`). That is what the compiler is warning you about. – mjwills May 07 '20 at 01:02
  • Hi guys, I updated the question with what I really want to achieve. I don't know if it's possible. The only solution I've come up with is to make enum Color public, but that's too permissive. – abe May 07 '20 at 01:03
  • Why **specifically** do you want `Color` to be `internal` rather than `public`? What is the downside of making it `public`? – mjwills May 07 '20 at 01:05
  • @mjwills as far as I know, is good programming practice to keep things as restrictive as possible, so you have less dependencies between classes. – abe May 07 '20 at 01:08
  • 1
    The _simplest_ solution is likely to use `protected enum Color {WHITE, BLACK};` and move that **inside** `ClassB`. – mjwills May 07 '20 at 01:10
  • This was a simplified sample of the problem I'm facing. In reality B and C are test classes, A is the production class. I can't move anything from A to B or C – abe May 07 '20 at 01:13
  • @mjwills I removed 'public' from all classes, and it works. I think this goes along the lines of your answer ' C has access to methodOne (since it is protected). But it doesn't have access to Color (since it is in a different assembly, and doesn't inherit from A)' – abe May 07 '20 at 01:16

0 Answers0