Inconsistent Accessibility error

c#, visual-studio-2008

Solution

Look at the following definition. Notice `Foo` is visible to anyone, and its public method `GetBar` is also visible to anyone who can see `Foo`:

public class Foo 
{ 
    public Bar GetBar() { return new Bar(); } 
}

Here's the definition of `Bar`:

internal class Bar {}

Notice `Bar` is internal to the assembly, whereas `Foo` is visible to all. `Foo` cannot expose `Bar` to the outside world, so the compiler throws this exception.

Another example would be:

public class Foo 
{ 
    public Foo.Bar GetBar() { return new Bar(); } 
    private class Bar {} 
}

`Bar` is a private class of `Foo` and can only be visible to instances of `Foo`. `Foo` cannot expose this type to the outside world, so the compiler throws the same exception.

Examples of refactoring:

Make the hidden type public

public class Bar {}
public class Foo { public class Bar {} }

Encapsulation

public class BarEncapsulator
{
  private Bar _bar;
  internal BarEncapsulator(Bar myBar) { _bar = myBar; }
  public string BarString { get { return _bar.MyString; } }
}

Hide everything

internal class Bar {}
internal class Foo { public class Bar {} }

Refactor it away

public class BarEncapsulator
{
  private string _barString;
  public string BarString { get { return _barString; } }
}

Problem

I tried upgrading an ASP.Net application from Visual Studio 2005 to 2008, and I tried compiling just to verify that it would compile and I received this error. ``` Error 1 Inconsistent accessibility: property type 'Web.Properties.UITitleSettings' is less accessible than property 'Web.Ctrl.BasePanel.UISettings' \\projectLocation\Ctrl\BasePanel.cs 25 43 ProjectName ``` (I removed the class path before Web.Properties and Web.Ctrl, it normally contains it) The piece of code its referencing is ``` public Properties.UITitleSettings UISettings { get { return _uiSettings; } } ``` I'm not quite sure what this error is attempting to say. Does the type need to be casted (invalid implicit cast between two incompatible types?) or is it a class override issue?

Original source