[RESOLVED] [2.0] Iterating through an enumeration's possible values
I've got a Type variable that is an enumeration, although i don't know which one. Is there an easy of listing it's possible values, along with their names?
Re: [2.0] Iterating through an enumeration's possible values
Quote:
Originally Posted by SLH
I've got a Type variable that is an enumeration, although i don't know which one. Is there an easy of listing it's possible values, along with their names?
If we have a ennumeration variable, then we can easily show its possible values by writing the name of the enumeration variable and after that place dot(.) you will see it's possible values of that enumeration type varialbe.
Imran Ahmad Mughal
Re: [2.0] Iterating through an enumeration's possible values
Quote:
Originally Posted by ahmad_iam
If we have a ennumeration variable, then we can easily show its possible values by writing the name of the enumeration variable and after that place dot(.) you will see it's possible values of that enumeration type varialbe.
Imran Ahmad Mughal
That will give you the name of each enumeration... cast it to integer to get the values
Re: [2.0] Iterating through an enumeration's possible values
Code:
foreach (string name in Enum.GetNames(typeof(MessageBoxButtons)))
{
MessageBox.Show("Name: " + name);
}
foreach (MessageBoxButtons value in Enum.GetValues(typeof(MessageBoxButtons)))
{
MessageBox.Show("Value (as string): " + value.ToString());
MessageBox.Show("Value (as int): " + (int)value);
}
Re: [2.0] Iterating through an enumeration's possible values
Thanks for the example, but what type is Enum?
I have my Type variable (which i know is an enum) which doesn't have a GetNames or a GetValues method.
Re: [2.0] Iterating through an enumeration's possible values
Enum IS the type. GetNames and GetValues are static methods of the Enum class. Notice that you pass the Type of your specific enumerated type to the method as a parameter. I've just used MessageBoxButtons as an example. You can pass your own enumerated Type instead.
Re: [2.0] Iterating through an enumeration's possible values
Ah ha, gottya. Didn't realise MessageBoxButtons was an enumeration!
Thanks for your help. :)