在C#中使用Enum

时间:2013-08-01 12:46:28

标签: winforms c#-4.0 enums

如何创建C#Enum来存储需要采用以下格式的数据?

假设我有一个小项目列表,我想将它们存储在枚举中。 我只见过如下例子:

enum myThing : int {
    None = 0,
    Read = 1,
    Write = 2,
    Modify = 4,
    Delete = 8
    Create = 16,
    All = Read | Write | Modify | Delete | Create
}

我需要存储的内容如下:

  • Item_Name(string)
  • Item_Is_Multi_coloured(boolean)
  • Item_Is_Expensive(boolean)

我如何声明一个将存储项目的上述三个属性的枚举?

5 个答案:

答案 0 :(得分:4)

你不能在枚举中存储字符串,你应该声明一个类:

  // The simplest MyItem version
  public class MyItem {
    public String Name {get; set}
    public Boolean Is_Multi_coloured {get; set}
    public Boolean Is_Expensive {get; set}
  }

您可以加入枚举的唯一选项是item_Is_Multi_coloured和item_Is_Expensive:

  [Flags()]
  public enum MyItemKind {
    None = 0,
    Is_Multi_coloured = 1,
    Is_Expensive = 2 
  } 

所以修改后的类MyItem将是

  // MyItem with enum (the simplest version)
  public class MyItem {
    public String Name {get; set}
    public MyItemKind Kind {get; set}
  }

答案 1 :(得分:1)

与Java不同,C#只能从数值类型(char,byte,long,int)继承枚举。如果你想要更复杂的东西,我建议使用一个“重”类和一个这样的枚举:

public enum MyThingSimple : int {
  Enum1 = 0,
  Enum2 = 1
}

public sealed class MyThingComplex {
  public string Name { get; private set; }
  public bool IsMultiColoured { get; private set; }
  public bool IsExpensive { get; set; }
  public MyThingSimple Value { get; private set; }

  public static readonly MyThingComplex Enum1 = new MyThingComplex {
    IsExpensive = false,
    IsMultiColoured = true,
    Value = MyThingSimple.Enum1,
    Name = "Enum1"
  };
  public static readonly MyThingComplex Enum2 = new MyThingComplex {
    IsExpensive = false,
    IsMultiColoured = true,
    Value = MyThingSimple.Enum2,
    Name = "Enum2"
  };
  private static readonly Dictionary<MyThingSimple, MyThingComplex> m_Mapping = 
    new Dictionary<MyThingSimple,MyThingComplex> {
      { MyThingSimple.Enum1, Enum1 },
      { MyThingSimple.Enum2, Enum2 }
  };

  public static MyThingComplex FromSimple(MyThingSimple simpleThing) {
    return m_Mapping[simpleThing];
  }
}

答案 2 :(得分:0)

枚举只允许一种类型,您是否尝试使用过Struct? (http://msdn.microsoft.com/en-us/library/vstudio/ah19swz4.aspx

答案 3 :(得分:0)

你可以创建如下布尔属性的枚举

enum myThing : int {
item_Is_Multi_coloured= 0,
item_Is_Not_Multi_coloured= 1,
item_Is_Expensive = 2,
item_Is_Not_Expensive = 4,

}

但您无法在名称属性

上创建枚举

所以看起来你想要一个来自你的物品对象的类

如果是这种情况,那么你可以这样做

class Item
{ 
string ItemName{get;set;}
bool IsMultiColoured{get;set;}
bool IsExpensive{get;set;} 
}

注意:班级作者是Tim Schmelter

答案 4 :(得分:0)

我建议只使用一个类和另一个带有返回定义的类对象的属性的静态容器类,例如。

class Item
{
    readonly bool IsMulticolored;
    readonly bool IsExpensive;
    readonly string Name;

    Item(string Name, bool IsMulticolored, bool IsExpensive)
    {
        me.Name = Name;
        me.IsMulticolored = IsMulticolored;
        me.IsExpensive = IsExpensive;
    }
}

static class Items
{
    const _itemA = new Item("thing", true, true);

    static Item ItemA
    {
        get{ return _itemA; }
    }
}

然后你获得了enum s的稳定性,你可以用自然的方式来引用它们,例如Items.ItemA.Name