Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a string value to an enums and then finding the enum by value

Tags:

c#

enums

I want to have an enum like the output below, and then have a method (something like Util.FindFruitByValue("A")) which returns the enum Apple. This is because the abbreviations are stored in a database and I need to convert them to appropriate enums after reading from the db.

This is like a lookup table, but the difference is the value is a string instead of an int. I am populating a business object by reading the values from database, and I would like to use a type with fixed values for the object property instead of a string.

Is this possible? Do I need to create a separate class for it?

public enum Fruit
{
    Apple = "A"
    Banana = "B"
    Cherry = "C"
}
like image 315
RKP Avatar asked Aug 17 '10 11:08

RKP


People also ask

Can we assign string to enum?

You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.

How do I convert string to enum?

Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.

Can we assign string value to enum in C#?

Using string-based enums in C# is not supported and throws a compiler error. Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in code to make your life easier. The most popular string enum alternatives are: Use a public static readonly string.

How do you find the value of an enum?

Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.


1 Answers

I solved the problem by using the Description attribute on the enum. the solution is as follows. I use the extension method to get the description. the code to get the description is taken from this link http://blog.spontaneouspublicity.com/post/2008/01/17/Associating-Strings-with-enums-in-C.aspx. thanks for your replies.

    public enum Fruit
{
    [Description("Apple")]
    A,
    [Description("Banana")]
    B,
    [Description("Cherry")]
    C
}

public static class Util
{
    public static T StringToEnum<T>(string name)
    {
        return (T)Enum.Parse(typeof(T), name);
    }

    public static string ToDescriptionString(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}
like image 62
RKP Avatar answered Nov 11 '22 11:11

RKP