4

I think this question needs some code:

private TypeValues GetEnumValues(Type enumType, string description)
        {
            TypeValues wtv = new TypeValues();
            wtv.TypeValueDescription = description;
            List<string> values = Enum.GetNames(enumType).ToList();
            foreach (string v in values)
            {
                //how to get the integer value of the enum value 'v' ?????
                wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
            }
            return wtv;

        }

Call it like this:

GetEnumValues(typeof(AanhefType), "some name");

In the GetEnumValues function i have the values of the enum. So i iterate the values and i want to get the integer value of that enum value also.

So my values are 'red' and 'green', and i also want to get 0 and 1.

When i have the Enum in my function, i can create the enum value from the string and cast it to that enum and then cast it to an int, but in this case, i don't have the enum itself, but only the type of the enum.

I tried passing in the actual enum as a parameter too, but i'm not allowed to pass an enum as a parameter.

So now i'm stuck.....

0

3 Answers 3

5
private TypeValues GetEnumValues(Type enumType, string description)
        {
            TypeValues wtv = new TypeValues();
            wtv.TypeValueDescription = description;
            List<string> values = Enum.GetNames(enumType).ToList();
            foreach (string v in values)
            {
                //how to get the integer value of the enum value 'v' ?????

               int value = (int)Enum.Parse(enumType, v);

                wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
            }
            return wtv;

        }

http://msdn.microsoft.com/en-us/library/essfb559.aspx

Enum.Parse will take a Type and a String and return a reference to one of the enum values - which can then simply be cast to int.

Sign up to request clarification or add additional context in comments.

1 Comment

this works! weird though, tried some code in the immediate window, and also this one, which did NOT show me the int value: (int)Enum.Parse(enumType, v) shows me 'De Heer' which is the value of the string 'v'. But when running the code, it DOES show me an int. Weird....
5

Try

(int)Enum.Parse(enumType, v)

1 Comment

+1 There is no need to loop through the values yourself, when there is a method that does that already.
0
public static class StringEnum
{


    public static string GetStringValue(Enum value)
    {
        string output = null;
        Type type = value.GetType();

        FieldInfo fi = type.GetField(value.ToString());
        StringValue[] attr = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];

        if (attr.Length > 0)
        {
            output = attr[0].Value;
        }
        return output;
    }
}

it is method to get the string value.

public enum CampaignRequestType { [StringValue("None")] None = 0, [StringValue("Pharmacy Cards")] Pharmacy_Cards = 1,[StringValue("Prospect Campaign")] Prospect_Campaign = 2,[StringValue("Tradeshow/Advertising")] Tradeshow_Advertising = 3 }

its a enum...

string item = StringEnum.GetStringValue((Enumeration.CampaignRequestType)updateRequestStatus.RequestType_Code);

here (Enumeration.CampaignRequestType) is my enumeration and updateRequestStatus.RequestType_Code is data base field int type

i cast int value to enumeration type

1 Comment

Welcome to StackOverflow ! try use more the editing tool for code so your answer will be more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.