Reputation: 469
I searched around the site and Google, but was unable to find quite what I was looking for. I think it's pretty simple, but I'm fairly new to C# so I'm having some troubles with this one.
Basically, I have an if statement to test the selection of Radio buttons I have, which is triggered on the click of a button. By default, I don't have any selected, and my program will notify me if I forget to select one, with a MessageBox. So essentially, you can't do anything until you have a selection made.
Up until now, I only had 3 radio buttons, but today I have added one more, and a bunch more code associated with the new one, including a new function.
I'm still trying to figure out what is the most memory efficient way to test for the radioButton.Check Now, is that with temporary variables, or with this if statement I have?
if (pRKButton.Checked != true ^ hexButton.Checked != true ^ binaryButton.Checked != true)
{
MessageBox.Show("Choose a method of encryption.");
return null;
}
else....//More Goes Here
My main question is this: Why when I add one more argument to the if statement, using exclusive or, like so;
if (pRKButton.Checked != true ^ hexButton.Checked != true ^ binaryButton.Checked != true ^ md5Button.Checked != true)
The program doesn't register me clicking ANY of the radio buttons.
At this point I'm considering making a listbox to hold all the values and select the choice, as the list will grow. But I'm super curious about the reasoning behind not being able to use exclusive or with more than 3 arguments. I'm not mathematician, by any degree, but I'd love to learn more.
Upvotes: 2
Views: 3625
Reputation: 838256
Before I get to the main part of the question, I want to point out that you don't actually need to use XOR here. When you have radio buttons only zero or one can be selected. When you select one, the others in the same group automatically become deselected. So you can use ||
to test if at least one is selected.
if (pRKButton.Checked || hexButton.Checked || binaryButton.Checked)
{
// At least one (i.e. exactly one) is checked.
}
else
{
// None checked.
}
To answer the main part of your question, your code fails because when you have one button selected you get false ^ true ^ true
(noting that you are testing if the buttons are unchecked). The result of this is false. But when you have four variables you instead have false ^ true ^ true ^ true
which is true.
XOR
does not mean "only one is true" as you seem to think. A XOR B XOR C
is true if an odd number of A, B, C are true. This is not the same as "exactly one" because it is true if all three of A, B and C are true.
See the following truth table to understand the behaviour of chained XOR with three inputs:
Upvotes: 8
Reputation: 1813
In addition to Mark Byer's good information about radio button functionality, you might be concerned that your code will get smelly with a very long if statement.
Try using LINQ to simplify:
container.Controls.OfType<RadioButton>().Any<RadioButton>(r => r.Checked);
Will be true or false based on any radio button being checked.
Upvotes: 2