H
Harris
Dear all,
I have the following codes:
======
public enum Enum_Value
{
Value0 = 0,
Value1 = 10,
Value2 = 5,
Value3 = -1
}
private void comboImageType_SelectedIndexChanged(object sender,
System.EventArgs e)
{
setCombo(comboValue, typeof(Enum_Value));
}
private void setCombo(ComboBox combo, Type enumType)
{
object val;
Enum_Value enumValueObject;
//only do this if combo selection is valid
if (combo.SelectedIndex >= 0)
{
//Get element of Enum_Value pointed by selected index.
//So let say if SelectedIndex is 2 then I should get 5
val = Enum.GetValues(enumType).GetValue(combo.SelectedIndex);
//Assign back this value to enumValueObject
//enumValueObject = val; <-- ERROR : Cannot implicitly convert type
'object' to ...
enumValueObject = (Enum_Value ) val; // <--- is this is OK?
int iVal = (int) enumValueObject;
Console.Write("Enum value is " + iVal.ToString());
}
}
======
This really start as a question.. But while typing the question I got
some idea, try it and get it to work. So I might have already solve the
problem.
Basically I have to get enum value from ComboBox selected item. As you
can see above my problem is I try to assign object to enumValueObject .
So the question now is, am I doing the right thing? Perhaps there is
better way or shorter way of doing things?
Thanks in advance.
I have the following codes:
======
public enum Enum_Value
{
Value0 = 0,
Value1 = 10,
Value2 = 5,
Value3 = -1
}
private void comboImageType_SelectedIndexChanged(object sender,
System.EventArgs e)
{
setCombo(comboValue, typeof(Enum_Value));
}
private void setCombo(ComboBox combo, Type enumType)
{
object val;
Enum_Value enumValueObject;
//only do this if combo selection is valid
if (combo.SelectedIndex >= 0)
{
//Get element of Enum_Value pointed by selected index.
//So let say if SelectedIndex is 2 then I should get 5
val = Enum.GetValues(enumType).GetValue(combo.SelectedIndex);
//Assign back this value to enumValueObject
//enumValueObject = val; <-- ERROR : Cannot implicitly convert type
'object' to ...
enumValueObject = (Enum_Value ) val; // <--- is this is OK?
int iVal = (int) enumValueObject;
Console.Write("Enum value is " + iVal.ToString());
}
}
======
This really start as a question.. But while typing the question I got
some idea, try it and get it to work. So I might have already solve the
problem.
Basically I have to get enum value from ComboBox selected item. As you
can see above my problem is I try to assign object to enumValueObject .
So the question now is, am I doing the right thing? Perhaps there is
better way or shorter way of doing things?
Thanks in advance.