I have an enum as
public enum Operation
{
Add = 1,
Substract = 2,
Multiply = 3,
Divide = 4
}
I have four radio buttons : Add, Subtract, Multiply, Divide.
Based on the selection, I want to return the corresponding Enum value. All my radio buttons are present in a groupbox.
I know this is a simple thing, but from long time I am not able to get it right. Thanks.
EDIT
Thats what I have tried….
public Operation Operation
{
get
{
foreach (Control control in gbxOperation.Controls)
{
var radioButton = control as radioButton;
if (radioButton != null && radioButton.Checked)
{
if(radioButton.Text.ToLower() == "add")
return Operation.Add;
if (radioButton.Text.ToLower() == "subtract")
return Operation.Substract;
if (radioButton.Text.ToLower() == "multiply")
return Operation.Multiply;
if (radioButton.Text.ToLower() == "divide")
return Operation.Divide;
}
}
return Operation.Add;
}
}
It’s not very clear from your question, but if you have a string like
"Add"and you want to convert it toOperation, you can useEnum.Parse(). Something like:But probably a better option would be to associate the radio buttons with the enum values directly, not through the text of the button. How exactly to do that depends on what kind UI library are you using.