PDA

Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Iterating through an enumeration's possible values


SLH
Jun 2nd, 2006, 09:51 AM
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?

ahmad_iam
Jun 2nd, 2006, 07:24 PM
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

ComputerJy
Jun 2nd, 2006, 07:27 PM
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

jmcilhinney
Jun 3rd, 2006, 03:36 AM
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);
}

SLH
Jun 3rd, 2006, 04:38 AM
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.

jmcilhinney
Jun 3rd, 2006, 06:28 AM
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.

SLH
Jun 3rd, 2006, 08:13 AM
Ah ha, gottya. Didn't realise MessageBoxButtons was an enumeration!

Thanks for your help. :)