17

I've a enum class like,

public enum USERTYPE
{
   Permanant=1,
   Temporary=2,
}

in my business object I just declare this enum as

private List<USERTYPE> userType=new List<USERTYPE>;

and in the get/set method, I tried like

public List<USERTYPE> UserType
    {
        get 
        {
            return userType;
        }
        set
        { 
            userType= value; 
        }
    }

here it returns the no of rows as 0, how can I get all the values in the Enum here, can anyone help me here...

6
  • Are you filling that list before reading the number of rows ? Commented Jun 28, 2012 at 7:55
  • 1
    @Shanish take alook at here msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx Commented Jun 28, 2012 at 7:55
  • Are you sure that you actually want a List of USERTYPE? Commented Jun 28, 2012 at 7:55
  • Enum.GetValues(typeof(USERTYPE)).Cast<USERTYPE>().ToList(); this works actually, thanks everyone Commented Jun 28, 2012 at 7:59
  • @davenewza ya I want a list of USERTYPE, its working,thanks for ur support Commented Jun 28, 2012 at 7:59

5 Answers 5

35

You can use this to get all individual enum values:

private List<USERTYPE> userTypes = Enum.GetValues(typeof(USERTYPE)).Cast<USERTYPE>().ToList();

If you do things like this more often, you could create a generic utility method for it:

public static T[] GetEnumValues<T>() where T : struct {
    if (!typeof(T).IsEnum) {
        throw new ArgumentException("GetValues<T> can only be called for types derived from System.Enum", "T");
    }
    return (T[])Enum.GetValues(typeof(T));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not very elegant since Enumerable.Cast enumerates your array, all members are boxed and then unboxed, and Enumerable.ToList enumerates and creates a new List<>. But I can understand why people use ugly solutions like this when the .NET Framework has not provided a generic version of Enum.GetValues.
To anyone who couldn't find Cast() or ToList(), this requires you to include System.Linq
6

GetValues returns System.Array, but we know it's really a TEnum[] (that is a one-dimensional array indexed from zero) where TEnum is USERTYPE in your case. Therefore use:

var allUsertypeValues = (USERTYPE[])Enum.GetValues(typeof(USERTYPE));

Comments

4

UserTypeCan you please try with that,

 UserType = Enum.GetValues(typeof(USERTYPE)).OfType<USERTYPE>().ToList();

Comments

2

If you want to get specific enum value from the list user.UserType then you first need to Add enum value to this list:

var user = new User();
//1 value - PERMANENT
user.UserType.Add(USERTYPE.Permanent);

But if you only need to get all the possible values from an arbitrary enum then you can try Enum.GetValues

//2 values - Permanant and Temporary
var enums = Enum.GetValues(typeof(USERTYPE));

Comments

2

What you have is basically List of enum. Not individual items inside that enum.

To get list of enum values you can do

string[] str = Enum.GetNames(typeof(USERTYPE));

To use in get/set return string[] instead of List<>

public string[] UserType
{
    get 
    {
        return Enum.GetNames(typeof(USERTYPE));
    }
}

I think set will not work here because you cannot add values in enum at runtime.

2 Comments

how can I use this in Get/Set
thanks Nikhil and pratap, for ur support, actually Botz solution is working for me

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.