Nasif
Nasif

Reputation: 31

enum with multiple values

In my model I have this code

public enum StockStatus
{       
    Origin= 1,

    [Display(Name = "In Transit")]
    InTransit = 5,
    [Display(Name = "Port Louis")]
    PortLouis = 6,
    Yard = 7,
    Requested = 8
}

I need to be able to have multiple values for Origin something like this

public enum StockStatus
{       
    Origin= 1,2,3,4,        

    [Display(Name = "In Transit")]
    InTransit = 5,
    [Display(Name = "Port Louis")]
    PortLouis = 6,
    Yard = 7,
    Requested = 8
}

i know it is not ok but i need to be able to do it this way

Upvotes: 3

Views: 8738

Answers (5)

ManishM
ManishM

Reputation: 593

The [Flags] attribute on an enum allows you to assign multiple values to your enum at once. You can do this with bitwise manipulations

[Flags]
public enum StockStatus
{
    Origin = 0x0 | 0x1 | 0x2,

    [Display(Name = "In Transit")]
    InTransit = 0x4,
    [Display(Name = "Port Louis")]
    PortLouis = 0x8,
    Yard = 0x10,
    Requested = 0x20
}

Upvotes: 1

Gauravsa
Gauravsa

Reputation: 6514

You would have to do something like this:

class Status
{
    static void Main(string[] args)
    {
        int code = 1;
        string name;
        Dictionary<int, string> StatusMap = new Dictionary<int, string>
        {
            { 1, "ORIGIN" },
            { 2, "ORIGIN" },
            { 3, "ORIGIN" },
            { 4, "IN TRANSIT" }
        };

   if (!StatusMap.TryGetValue(code, out name))
   {
       Console.WriteLine(name);
       // Error handling here
   }
  }
}

// or a method like this
public static string GetStatus(int code)
{
    string name;
    if (!StatusMap.TryGetValue(code, out name)
    {
        // Error handling here
    }
    return name;
}

Upvotes: 0

JunJie Wang
JunJie Wang

Reputation: 470

it is impossible, because following will be ambiguity.

StockStatus aStatus = StockStatus.Origin;

if am i, i will define Origin and OriginMax.

Origin= 1,
OriginMax= 4,

Usage:

1. StockStatus aStatus = StockStatus.Origin;
2. if (aStatus >= StockStatus.Origin && aStatus <= StockStatus.OriginMax)

Upvotes: 0

Nitin S
Nitin S

Reputation: 7591

You can't have comma separated values with enums.

You can do something like this:

public class StockStatus
{       
    public List<int> Origin= new List<int>(){1,2,3,4};

    [Display(Name = "In Transit")]
    public List<int> InTransit = new List<int>(){ 5};
    [Display(Name = "Port Louis")]
    public List<int> PortLouis = new List<int>(){ 6};
    public List<int> Yard = new List<int>(){ 7};
    public List<int> Requested = new List<int>(){ 8};
}

this way you will be able to have multiple values for Origin

Upvotes: 1

kurakura88
kurakura88

Reputation: 2305

You need to have the enum as Flags and the value needs to be bitwise unique(power of 2).

[Flags]
public enum MyEnum
{

    Origin= no1 | no2 | no3 | no4,

    no1 = 1,
    no2 = 1 << 1,
    no3 = 1 << 2,
    no4 = 1 << 3,

    [Display(Name = "In Transit")]
    InTransit = 1 << 4,
    [Display(Name = "Port Louis")]
    PortLouis = 1 << 5,
    Yard = 1 << 6,
    Requested = 1 << 7
}

Upvotes: 6

Related Questions