替代嵌套枚举 [英] Alternative to nesting enums

查看:168
本文介绍了替代嵌套枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建多个枚举因此,给人的句法 Dropdown.Category.Subcategory 。不过,我一直在读,这不是一个好主意。我的这个选择是主要是因为我想不出任何其他方式来选择取决于类别的选择枚举值,然后子类别的选择是不同的受所选择的枚举根据枚举值。



有没有更好的方式来创建这样的功能?我宁愿能够轻松识别这两个 .Category .Subcategory 名字,这将是一个奖金如果这个代码是可读的。



只是要清楚,我希望能够选择类别,有那么一个合适的类别的选择。

 公共类下拉
{
公共枚举礼物
{
GreetingCards,
VideoGreetings,
UnusualGifts,
ArtsAndCrafts,
HandmadeJewelry,
GiftsforGeeks,
PostcardsFrom,
RecycledCrafts,
其他
}
公共枚举GraphicsAndDesign
{
CartoonsAndCaricatures,
LogoDesign,
插图,
EbookCoversAndPackages,
WebDesignAndUI,
PhotographyAndPhotoshopping,
PresentationDesign,
FlyersAndBrochures,
BusinessCards,
BannersAndHeaders,
建筑,
LandingPages,
其他
}
}


解决方案

创建不能从外部继承的类,给它一些内部类,分别来自其扩大。然后添加静态只读变量对每个值要代表:

 公共类下拉
{
私人字符串值;

//防止外部继承
私人下拉(字符串值)
{
THIS.VALUE =价值;
}

公共类礼品:下拉
{
//防止外部继承
私人礼品(字符串值):基地(价值){}

酒店的公共静态只读下拉GreetingCards =
新礼品(GreetingCards);
公共静态只读下拉VideoGreetings =
新礼品(VideoGreetings);
公共静态只读下拉UnusualGifts =
新礼品(UnusualGifts);
公共静态只读下拉ArtsAndCrafts =
新礼品(ArtsAndCrafts);
}
公共类GraphicsAndDesign:下拉
{
//防止外部继承
私人GraphicsAndDesign(字符串值):基地(价值){}

酒店的公共静态只读下拉CartoonsAndCaricatures =
新GraphicsAndDesign(CartoonsAndCaricatures);
公共静态只读下拉LogoDesign =
新GraphicsAndDesign(LogoDesign);
公共静态只读下拉插图=
新GraphicsAndDesign(图);
}

公共重写字符串的ToString()
{
返回值;
}
}

在这种情况下,每一个值实际上是一个实例键入下拉,所以你可以有,比方说,一个参数一个接受下拉实例的方法。随着枚举没有办法说,我想接受任何在下拉类声明的枚举。



下面是一些示例用法:

 公共静态无效UseDropdown(下拉式)
{
如果(类型为Dropdown.Gifts)
{
如果(类型== Dropdown.Gifts.GreetingCards)
{
DoStuff();

}
}
,否则如果(类型为Dropdown.GraphicsAndDesign)
{
}
}

您还可以有接受类型的对象参数礼品 GraphicsAndDesign ,如果你只想要一个子类型在某些情形下有效。



不幸的是,采用这种解决方案有没有好办法开关在下拉值;你必须只使用if 链检查值如果 / 其他。



使用实例字符串值不得要求(见第一次修订如果没有它的一个版本),但它可以是能够有一个有意义的字符串值(或其他种类的价值非常有帮助的;你可以一个整数,字节,或任何与每个枚举值关联)。



等于如果离开而不被重写的GetHashCode 的实施应该是有意义的。



您可以实施 IComparable的如果项目应在逻辑上莫名其妙地订购,像真正的枚举。


I'm trying to create several enums as such, that gives the syntax of Dropdown.Category.Subcategory. However, I have been reading that this isn't such a good idea. My choice for this was mostly because I couldn't think of any other way to select different enum values depending on the choice of the category, and then the choice of the subcategory is subject to the selected enum based on the enum values.

Is there a better way to create such functionality? I would prefer to be able to easily identify both the .Category and .Subcategory names, and it would be a bonus if this code was readable.

Just to make it clear, I want to be able to choose the Category, then have an appropriate Subcategory selection.

public class Dropdown
{
    public enum Gifts
    {
        GreetingCards,
        VideoGreetings,
        UnusualGifts,
        ArtsAndCrafts,
        HandmadeJewelry,
        GiftsforGeeks,
        PostcardsFrom,
        RecycledCrafts,
        Other
    }
    public enum GraphicsAndDesign 
    {
        CartoonsAndCaricatures,
        LogoDesign,
        Illustration,
        EbookCoversAndPackages,
        WebDesignAndUI,
        PhotographyAndPhotoshopping,
        PresentationDesign,
        FlyersAndBrochures,
        BusinessCards,
        BannersAndHeaders,
        Architecture,
        LandingPages,
        Other
    }
}

解决方案

Create a class that cannot be inherited from externally, give it several inner classes, each extending from it. Then add static read only variables for each of the values that you want to represent:

public class Dropdown
{
    private string value;

    //prevent external inheritance
    private Dropdown(string value)
    {
        this.value = value;
    }

    public class Gifts : Dropdown
    {
        //prevent external inheritance
        private Gifts(string value) : base(value) { }

        public static readonly Dropdown GreetingCards =
            new Gifts("GreetingCards");
        public static readonly Dropdown VideoGreetings =
            new Gifts("VideoGreetings");
        public static readonly Dropdown UnusualGifts =
            new Gifts("UnusualGifts");
        public static readonly Dropdown ArtsAndCrafts =
            new Gifts("ArtsAndCrafts");
    }
    public class GraphicsAndDesign : Dropdown
    {
        //prevent external inheritance
        private GraphicsAndDesign(string value) : base(value) { }

        public static readonly Dropdown CartoonsAndCaricatures =
            new GraphicsAndDesign("CartoonsAndCaricatures");
        public static readonly Dropdown LogoDesign =
            new GraphicsAndDesign("LogoDesign");
        public static readonly Dropdown Illustration =
            new GraphicsAndDesign("Illustration");
    }

    public override string ToString()
    {
        return value;
    }
}

In this case every single value is actually an instance of type Dropdown, so you could have, say, a parameter to a method that accepts a Dropdown instance. With enums there is no way to say, "I want to accept any of the enums declared in the Dropdown class."

Here is some example usage:

public static void UseDropdown(Dropdown type)
{
    if (type is Dropdown.Gifts)
    {
        if (type == Dropdown.Gifts.GreetingCards)
        {
            DoStuff();

        }
    }
    else if (type is Dropdown.GraphicsAndDesign)
    {
    }
}

You could also have a parameter that accepts an object of type Gifts or GraphicsAndDesign, if you only want a sub-type to be valid in some context.

Sadly, using this solution there's no good way to switch on a dropdown value; you have to just use if/else if chains to check the values.

The use of an instance string value may not be required (see the first revision for a version without it) but it can be very helpful to be able to have a meaningful string value (or other kind of value; you can associate an integer, a byte, or whatever with each enumeration value).

The Equals and GetHashCode implementations should be meaningful if left without being overridden.

You can implement IComparable if the items should be logically ordered somehow, like real enums.

这篇关于替代嵌套枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆