1

What is wrong with this code ?

public ScreenManager _ScreenManager
        {
            get { return screenManager; }
            internal set { screenManager = value; }
        }
        ScreenManager screenManager;

and I get this error:

Inconsistent accessibility: field type 'ScreenSystem.ScreenManager' is less accessible than field 'ScreenSystem.Screen.ScreenManager'
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
user1357067
  • 11
  • 1
  • 1
  • 2
  • 6
    Worth noting: it's probably not a great idea to start the name of a public property with an underscore. Underscores are generally reserved for private fields (local variables). – Robert Harvey Apr 25 '12 at 19:46
  • Can you post some more code? It's kind of hard to get the full picture from what you've posted. – James Johnson Apr 25 '12 at 19:47

3 Answers3

12

I'm going to assume the type ScreenManager is internal while the class containing your property is public.

The compiler is saying that a publicly-accessible field (ScreenSystem.Screen.ScreenManager) is of a type (ScreenSystem.ScreenManager) that isn't publicly-accessible.

Your field should generally be private anyway. And you might be missing a public in front of your class ScreenSystem.ScreenManager (it defaults to internal IIRC).

antijon
  • 982
  • 5
  • 7
4

Here is your scenario:

namespace ScreenSystem
{
    internal class ScreenManager
    {
        public string Test { get; set; }
    }
}

namespace ScreenSystem
{
    public class Screen
    {
        public ScreenManager Manager
        {
            get; internal set;
        }
    }
}

Compiler Output
Inconsistent accessibility: property type 'ScreenSystem.ScreenManager' is less accessible than property 'ScreenSystem.Screen.Manager'

Here are the solutions, depending on what you're trying to do:

  • Make the Screen class internal
  • Make the ScreenManager class public
  • Make the ScreenManager class public and the Screen class internal
  • Make the Screen.Manager property internal (and remove the internal set accessor)

Either one of the above will compile without errors. It really just depends on what you're trying to achieve.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • @RobertHarvey: What do you mean? Are you agreeing with my answer? – James Johnson Apr 25 '12 at 19:46
  • My guess is that `ScreenSystem.Screen.ScreenManager` is referring to class `ScreenManager` in namespace `ScreenSystem.Screen` and `ScreenSystem.ScreenManager` references the property `ScreenManager` in class `ScreenManager` (fully qualified to `ScreenSystem.Screen.ScreenManager`). There is only one class, therefore no namespace issue. It's just confusing because the class has the same name as the property. – Servy Apr 25 '12 at 19:56
2

It means that you have ScreenManager set up as private or protected yet you are are trying to make a property public that relies on ScreenManager. You can't do that.

Make ScreenManager public or make _ScreenManager the same accessibility as ScreenManager.

Michael Todd
  • 16,679
  • 4
  • 49
  • 69