12

I have an enum

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

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

2
  • Your numbering scheme looks strange. are you sure you want those to be int, and not octals? This suite of number also call for a FlagsAttribute. Commented Feb 22, 2013 at 8:38
  • 1
    Your question is nat clear for me. You want to have string "010" or value of ProductionStatus.Validated as int? Both cases are in my answer. If you want something else, just write it clearly. Commented Feb 22, 2013 at 8:51

6 Answers 6

15

Just to throw another solution in there...

((int)ProductionStatus.Validated).ToString("D3"); 
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

No, it will return 10 but I need 010. So I have to parse it to string, but can't find out how to do it without parsing it to int first.
Then you have to use strings. You can't use a numeric type alone to include formatting like that. A number is just a number.
okey, i have been thinking that i can directly parse it to string somehow. Thanks for help.
wtf are you talking about, it's only an example.
I'm saying that you can generate the string using number formatting. It avoids duplicating all keys and values, which is always bad.
4

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

3 Comments

yeah, I typed it too fast. fixed.
you are missing a bracket after Validated. Can't edit the post as it is less than 6 chars :(
@markoo, thanks. fixed. stackoveflows needs a compiler hooked in to detect those mismatches :)
3
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.

1 Comment

Why -1? That will get all of the values of the enum into a dictionary and then you can get by key. Which is exactly what was asked.
3

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

Comments

1

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.