Enum Question

  • Thread starter Thread starter CuMPeEWeEr [No MCSD]
  • Start date Start date
C

CuMPeEWeEr [No MCSD]

public enum Color {Red=1; Yellow=2; Orange=3; }

Is there a way to get the value of the Color easily? I know it can be done
using Enum.GetValues(typeof(Color)) but I have to use a for-loop to get the
value in this case. Is there a direct way to get the value? something like
Enum.GetValue(Color.Red) and returns 1?

Thanks..
 
Hi,

If I understand your question correctly, you what to get the constant that
represents an enum. To do this you can cast the enum to an integer.

int i = (int)Color.Red;

Hope this helps
 
Back
Top