Home > OS >  Different level of accessors for class and enum
Different level of accessors for class and enum

Time:01-08

I need to keep my enumeration internal and I need to keep my class style public. How to handle in this situation?

public class Style : VisionItem, BaseItem
{
    public string TargetType { get; set; }
    public string FontFamily { get; set; }
    public int FontSize { get; set; }
    public StyleType Type { get; set; }
}

internal enum StyleType
{
    TextCss,
    TextJs
}

I received such error: Inconsistent accessibility: property type 'StyleType' is less accessible than property 'Style.Type'

CodePudding user response:

The type Style can be public, but external code can't see the Type property. eg

public class Style : VisionItem, BaseItem
{
    public string TargetType { get; set; }
    public string FontFamily { get; set; }
    public int FontSize { get; set; }
    internal StyleType Type { get; set; }
}

internal enum StyleType
{
    TextCss,
    TextJs
}

CodePudding user response:

You can declare enum like this:

internal enum StyleType : int {
    TextCss,
    TextJs
}

and Style with Type as int propety that set StyleType local variable

private StyleType type;
public int Type {
    get => (int) type;
    /*internal*/ set {
        if(Enum.IsDefined(typeof(StyleType), value))
            type = (StyleType) value;
        else
            throw new ArgumentOutOfRangeException();
    }
}    
  • Related