开发者

Inconsistent Accessibility error

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?


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:

  1. Make the hidden type public

    public class Bar {}
    public class Foo { public class Bar {} }
    
  2. Encapsulation

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

    internal class Bar {}
    internal class Foo { public class Bar {} }
    
  4. Refactor it away

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


By default, the interface will be like this

Interface ICourseRepository{

}

Change it to

Public Interface ICourseRepostory{

}


Removing the "class path" won't do anything. You're just making your code less verbose... Check the definition of Properties.UITitleSettings. It'll be private or protected etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜