Get value from C # enums

I have an enumeration

public enum ProductionStatus { Received = 000, Validated = 010, PlannedAndConverted = 020, InProduction = 030, QAChecked = 040, Delivered = 070, RejectedOrCancelled = 100 } 

I need to get the key value from this listing, for example, when choosing ProductionStatus.Validated it should return 010. How can I do this?

+6
source share
6 answers

Just drop another solution ...

 ((int)ProductionStatus.Validated).ToString("D3"); 
+7
source

With formatting:

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

It is short and simple, and it returns a string.

You can include this in the extension method if you like

 public static class ProdStatusExtensions { public static string (this ProductionStatus status) { return ((int)status).ToString ("000", CultureInfo.InvariantCulture); } } 
+4
source
 var code = (int)ProductionStatus.Validated; 

You can also convert int to an enum value, for example:

 var status = (ProductionStatus)10; 

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

If you want 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"; } } 
+3
source
 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 the values ​​and names from an enumeration like this.

+1
source

In general, there is an Enum Class that contains an array of methods that make working with enumerations easier.

Here, if you want to use an enumerated value for an integer or another type, you can write:

 int validatedAsInt = (int) ProductionStatus.Validated 

validatedAsInt will contain the value ProductionStatus.Validated.

If you want to get digits like "010", you can write:

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

Or:

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

validatedAsString will contain "010".

+1
source

Here is a universal helper class that will do the opposite - get the 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: if we have this Enum :

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

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

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

So, you can insert any Enum object and get it by value:

 var enumObject = EnumHelpers.GetEnumObjectByValue<YOUR_ENUM_TYPE>(VALUE); 
+1
source

All Articles