get value from c# enums

后端 未结 6 2018
无人共我
无人共我 2021-01-18 05:39

I have an enum

public enum ProductionStatus {
    Received = 000,
    Validated = 010,
    PlannedAndConverted = 020,
    InProduction = 030,
    QAChecked          


        
相关标签:
6条回答
  • 2021-01-18 05:53
    var enumValues = Enum.GetValues(typeof(ProductionStatus)).Cast<object>()
                                    .ToDictionary(enumValue => enumValue.ToString(), enumValue => (int)enumValue);
    
    foreach (var enumValue in enumValues)
    {
        Console.WriteLine("item: {0}, value: {1}", enumValue.Key, enumValue.Value.ToString("000");
    }
    

    You can get all of the values and names from an enum like so.

    0 讨论(0)
  • 2021-01-18 06:01

    With Formatting:

    ((int)ProductionStatus.Validated).ToString("000",  CultureInfo.InvariantCulture);
    

    That's short and simple, and it returns a string.

    You can factor that into an extension method if you like

    public static class ProdStatusExtensions {
        public static string (this ProductionStatus status) {
            return ((int)status).ToString ("000",  CultureInfo.InvariantCulture);
        }
    }
    
    0 讨论(0)
  • 2021-01-18 06:02
    var code = (int)ProductionStatus.Validated;
    

    You can also convert an int to an enum value, like this:

    var status = (ProductionStatus)10;
    

    bool eq = 010 == 10; they are actually equal

    If you would like to use strings , use this method.

        static string EnumToString(ProductionStatus val)
        {
            switch (val)
            {
                case ProductionStatus.Received:
                    return "000";
                case ProductionStatus.Validated:
                    return "010";
                case ProductionStatus.PlannedAndConverted:
                    return "020"; 
                default:
                    return "Unknown value";
            }
        }
    
    0 讨论(0)
  • 2021-01-18 06:03

    In general there is an Enum Class that contains an array of methods facilitating the work with enums.

    Here, if you want to cast enumerable value to integer or other type, you can write:

    int validatedAsInt = (int) ProductionStatus.Validated
    

    validatedAsInt will contain value of ProductionStatus.Validated.

    If you want to obtain numbers like "010" you can write:

    string validatedAsString = ((int) ProductionStatus.Validated).ToString("000");
    

    Or:

    string validatedAsString = ((int) ProductionStatus.Validated).ToString("D3");
    

    validatedAsString will contain "010".

    0 讨论(0)
  • 2021-01-18 06:03

    Here is universal helper class that will do reverse action - getting key by value from ANY Enum:

    public static class EnumHelpers {
    
      public static T GetEnumObjectByValue<T>(int valueId) {
        return (T) Enum.ToObject(typeof (T), valueId);
      }
    
    }
    

    And it works like this - given we have this Enum:

    public enum ShipmentStatus {
      New = 0,
      Shipped = 1,
      Canceled = 2
    }
    

    So, to get Enum object ShipmentStatus.Shipped this will return this object:

    var enumObject = EnumHelpers.GetEnumObjectByValue<ShipmentStatus>(1);
    

    So basicaly you can stick any Enum object and get it by value:

    var enumObject = EnumHelpers.GetEnumObjectByValue<YOUR_ENUM_TYPE>(VALUE);
    
    0 讨论(0)
  • 2021-01-18 06:15

    Just to throw another solution in there...

    ((int)ProductionStatus.Validated).ToString("D3");
    
    0 讨论(0)
提交回复
热议问题