Enable/Disable checkBox based on combobox value with ternary operator
I'm trying to enable checkbox based on comboxbox selection. I am aware that with simple if condtion can solve my problem but when i change it to ternary operator it shows error
if (ComboBox.SelectedValue.Equals(enum.value))
{
chkbox.Enabled = false;
}
else
{
chkbox.Enabled = true;
}
Ternary:
ComboBox.SelectedValue == enum.value ? chkbox.Enabled = false : chkbox.Enabled = true;
This works fine but when i changed it to ternary it throws errors. How can i change it to make it work
Thanks in Advance.
Solution 1:
Your ternary syntax is incorrect. It should be like this instead:
chkbox.Enabled = ComboBox.SelectedValue == enum.value ? false : true;
You could also write it like this:
chkbox.Enabled = ComboBox.SelectedValue != enum.value;