我需要将选定的Enum值从Combobox(cbShowVal)传递到另一个类的帮助。

class A{
//Enum:
public enum myDisplayOptions{Test1, Test2, Test3}

//Combox binding:
cbShowVal.DataSource = Enum.GetValues(typeof(DisplayOptions));
}

class B{
private int newVal = 0;

public int GetNumOfSeats(myDisplayOptions ch){
swith(ch)
{
case myDisplayOptions.Test1:
newVal = 100;
break;

case myDisplayOptions.Test2:
newVal = 200;
break;

case myDisplayOptions.Test3:
newVal = 300;
break;

}
return newVal;
}
}

最佳答案

您将字符串转换回枚举

myDisplayOptions option =
    (myDisplayOptions)Enum.Parse(typeof(myDisplayOptions), cbShowVal.SelectedValue);


接着

B b = new B();
int seats = b.GetNumOfSeats(option);

07-28 03:59