Barry Kelly wrote:
There are at least 2 areas of confusion in your post:
(1) The values returned by Enum.GetValues( ) aren't necessarily in the
declared order.
Thanks, I dont realize this untill you point it out.
Fortunately my code so far do not depend on this since I do
Enum2Combo() and then later Combo2Enum(). So the order stay the same
during the process. My code is at the end of this post.
(2) You don't need to cast an object to an enumeration value before
casting it to an integer in order to get the underlying integral value.
Perhaps you will find this example illustrating:
I find it very illustrating :) . I have run this code changing Value3
to 1. It appeares that enum members are sorted according to their
value.
So if Value3 = 1. They are sorted to The ouput is
Value0 : 0
Value3 : 1
Value2 : 5
Value1 : 10
If Value3 = -1 and Value0 is -5
Value2 : 5
Value1 : 10
Value0 : -5
Value3 : -1
Beats me why it is sorted in such way. Ofcourse the bonus question is
whether they should be sorted at all. This is annoying.
---8<---
using System;
enum Enum_Value
{
Value0 = 0,
Value1 = 10,
Value2 = 5,
Value3 = -1
}
public class App
{
static void Main()
{
foreach (string name in Enum.GetNames(t ypeof(Enum_Valu e)))
Console.WriteLi ne(name);
Console.WriteLi ne("---");
object x = Enum.GetValues( typeof(Enum_Val ue)).GetValue(2 );
Console.WriteLi ne(x);
Console.WriteLi ne(x.GetType()) ;
Console.WriteLi ne((int) x);
}
}
--->8---
It prints:
---8<---
Value0
Value2
Value1
Value3
---
Value1
Enum_Value
10
--->8---
-- Barry
--
http://barrkel.blogspot.com/
=============== =
private enum Enum_Value
{
Value0 = 0,
Value1 = 10,
Value2 = 5,
Value3 = -1
}
private Enum_Value enumObject;
private void Form1_Load(obje ct sender, System.EventArg s e)
{
Enum2Combo(comb oBox1, typeof(Enum_Val ue), Enum_Value.Valu e3);
}
private object Combo2Enum(Comb oBox combo, Type enumType)
{
if (combo.Selected Index >= 0)
{
try
{
return Enum.ToObject(
enumType,
(int)(Enum.GetV alues(enumType) .GetValue(combo .SelectedIndex) ));
}
catch (Exception ex)
{
throw new Exception("Erro r setting enum from combo index. "
+ ex.Message, ex);
}
}
throw new Exception("Erro r setting enum due to invalid combo
index.");
}
private void Enum2Combo(Comb oBox combo, Type enumType, object
defaultValue)
{
int i=0;
foreach(string itemName in Enum.GetNames(e numType))
{
combo.Items.Add (itemName.Repla ce("_"," "));
Console.WriteLi ne(defaultValue .ToString());
if (itemName == defaultValue.To String())
combo.SelectedI ndex = i;
i++;
}
}
private void button1_Click(o bject sender, System.EventArg s e)
{
enumObject = (Enum_Value) Combo2Enum(comb oBox1, typeof(Enum_Val ue));
Console.WriteLi ne("---");
Console.WriteLi ne(enumObject.T oString() + " : " +
((int)enumObjec t).ToString());
}
=============== =
It does look over kill :D. But I learn a lot about enum while doing the
code. At the same time I am still somewhat dissatisfied with this code.
If I ever change it to something else, I will post some update here.
:D Much ado about nothing.
Take care.