我刚刚遇到这个问题,我的课程中有2 RadioButton
,每个RadioButton
的值应该从class-file-Enum中读取,
让我们说:
Enum Status {
Good,
Bad
}
我从我的Main
调用值没有问题,但我试图把它放到App.Config中这样:
<add key="ClassRadioButton1Status" value="Good"/>
<add key="ClassRadioButton2Status" value="Bad"/>
我还为我的app.Config创建了一个通用类:
public static string ClassRadioButton1Status= ConfigurationManager.AppSettings["ClassRadioButton1Status"];
public static string ClassRadioButton2Status= ConfigurationManager.AppSettings["ClassRadioButton2Status"];
现在在Main
内,我想这样调用RadioButton
并从枚举中分配值:
Public void processing (){
if (RadioButton.Checked == True ){
// here i have to Assign the Value from Enum to This:
Class.eStatus =// how to do from here?!!!!!!
}
}
答案 0 :(得分:0)
这可以通过以下方式实现(如Matt所说):
Public void processing (){
if (RadioButton.Checked == True ){
// here i have to Assign the Value from Enum to This:
Class.eStatus =(Status) Enum.Parse(typeof(Status), ClassRadioButton1Status);
}
}
注意强>
如果有人给AppSettings写了一些垃圾,你也应该检查Enum.IsDefined