Search code examples
c#access-modifiers

Cannot access internal classes outside of DLL & certain public variables aren't accessible


I'm having a hard time making this work.

The 3 classes FooType, WebApp & IWebApp must not be accessbile \ visible outside of this DLL. So hence the sealed & internal classes.

Issues I'm having are ...

1) In WebApp class, FeeType1 is not accessible in RouteOneBuilder method's parameter.

2) In WebApp class, FeeType1 is not accessible \ visible in switch's case-statement. (need to be visible).

3) In WebApp class, CreditApplication of FeeType1 property is not visible in the switch's case-statement (need to be visible).

Is there a better way to this complicated script? Am I already screwed for exposing classes outside of this DLL? Can all of step 1 to 4 be resolved differently (or be fixed somehow)?

I don't see how can I make this any simplier.

internal static class FooType
{
    public class FeeType
    {
        public FeeType() { }
        public string CreditApplication = "Credit Application";
        public string CreditVehicle = "Credit Vehicle";
    }
    public FeeType FeeType1
    {
       get { return new FeeType(); }
       private set { }
    }
}    
sealed class WebApp : IWebApp
{
    public string RouteOneBuilder(FooType.FeeType1 typing)
    {
       var xml = "";

       switch(typing)
       {
           case FooType.FeeType1.CreditApplication:
               xml = "asdf";
               break;
           default:
               throw new Exception("Unknown value");
       }

       return xml;
    }
}
internal interface IWebApp  
{
    string RouteOneBuilder(FooType.FeeType typing);
}

Solution

  • Your definition of a sealed class is incorrect. It is not an access modifier like public, private, protected and internal. Marking a class sealed only says that it cannot be inherited from; it does not say anything about access per se.

    From the MSDN documentation:

    When applied to a class, the sealed modifier prevents other classes from inheriting from it.

    That means that you can still provide a public class that is sealed. However, if you try to inherit from a sealed class, you will receive a compiler error like this:

    cannot derive from sealed type 'YourNamespace.YourSealedClass'.


    Also, I suggest you read this and this regarding internal/public and nested classes.

    Now, looking at the code you provided, the following compiler errors pop up:

    FooType.FeeType1': cannot declare instance members in a static class

    This error means that if the class is declared static, all of the members must be static too.

    FooType.FeeType1' is a 'property' but is used like a 'type'

    This arises from the fact that the class is static but none of the members are.

    Inconsistent accessibility: parameter type 'FooType.FeeType' is less accessible than method 'IWebApp.RouteOneBuilder(FooType.FeeType)'

    The return type and each of the types referenced in the formal parameter list of a method must be at least as accessible as the method itself.

    You can find more information about the last error here.