Click here to Skip to main content
15,917,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everybody!

I have the following:
1. enum
C#
[StringValueAttribute("Value 1")]
Value1=1,
[StringValueAttribute("Value 2")]
Value2,
[StringValueAttribute("Value 3")]
Value3,
[StringValueAttribute("Value 4")]
Value4,
[StringValueAttribute("Value 5")]
Value5

2.
public class StringValueAttribute : System.Attribute
{
    private string _value;
    public StringValueAttribute(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}

3. I have a column in my recordset that can have a possible range of 1 - 5 (tinyint / byte)
resultset.valuemarker


Is there any way I can convert this valuemarker into the corresponding string from the enum above?

Any help is appreciated :-D

Thanks
Reborndeveloper
Posted
Updated 11-Aug-10 8:55am
v2

You should be able to cast your int to your enum value and then use reflection to get the StringValueAttribute.

Something like this:

MyEnum myEnum = (MyEnum)myInt;
FieldInfo fi = (typeof(MyEnum).GetField(myEnum.ToString());
StringValueAttribute[] attrb = (StringValueAttribute[])fi.GetCustomAttributes(typeof(StringValueAttribute, false);

// If you only ever have 1 StringValueAttribute per enum value then 
// attrb[0] will contain that object.

return attrb[0].Value;
 
Share this answer
 
v2
Comments
Nish Nishant 11-Aug-10 19:05pm    
Reason for my vote of 5
Proposed as answer
 
Share this answer
 
Comments
Wjousts 11-Aug-10 15:31pm    
I think you misunderstood the question. The OP wants, for a given enum value the string in the attribute they attached to it.
AspDotNetDev 11-Aug-10 15:43pm    
Indeed, I missed the second part. I gave your answer a 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900