3

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

3
  • You can use [Flags] attribute Commented Aug 28, 2018 at 6:57
  • you can use class instead of enum, I wonder why you want to do that with enums? Commented Aug 28, 2018 at 7:06
  • "Consider" enum as an integer - you are saying "can I save 1,2,3,4 into 1?" Commented Aug 28, 2018 at 7:15

5 Answers 5

6

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

could you post a use case? how do you choose no2 as Origin? MyEnum enumValue = MyEnum.???
Thank you for your help but it is not displaying origin but it is displaying no1,no2,no3,no4 instead of Origin.
@Nasif perhaps put [Display(Name= "Origin")] attribute for all no1~4?
@MongZhu To be honest, I am not sure with your question, but your question's answer is simply MyEnum.no2 which is a member of MyEnum.Origin.
1

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

Comments

1

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
}

Comments

0

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)

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.