简体   繁体   中英

get value from c# enums

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?

只是在那里抛出另一个解决方案......

((int)ProductionStatus.Validated).ToString("D3");

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM