Reputation: 45
i working in asp.net, i am not very familiar with html and c# since the prof really doesnt go over much until it comes up in class.. i do understand it is very similar to java which i am quite familiar with so im not sure what my error is
i have an if statement for a click event to check the selected index of the checkbox list to make sure the right answers are chosen. it works somewhat right but for some reason whenever the first index is selected it makes it right which i dont understand. so really my issue is the selection of the first index causing some sort of issue with the if statement
here is my code for the event handler of the button..
if (DropDownList1.SelectedIndex.Equals(1)) {
LabelResult1.Text = "Question 1 Correct";
}
else {
LabelResult1.Text = "Question 1 Incorrect";
}
if (RadioButtonList1.SelectedIndex.Equals(1)) {
LabelResult2.Text = "Question 2 Correct";
}
else {
LabelResult2.Text = "Question 2 Incorrect";
}
if (CheckBoxList1.SelectedIndex.Equals(0&2&3)) {
LabelResult3.Text = "Question 3 Correct";
}
else {
LabelResult3.Text = "Question 3 Incorrect";
}
//write if statement to create image for fireworks
if(DropDownList1.SelectedIndex.Equals(1)&&RadioButtonList1.SelectedIndex.Equals(1)&&CheckBoxList1.SelectedIndex.Equals(0 & 2 & 3)) {
ImageFireworks.ImageUrl = "Images/giphy.gif";
}
i also have it so the gif appears if all questions are answered correctly. same the if statement, same issue is happening with it that anytime the 0 index of the checkbox is selected, they appear
Upvotes: 0
Views: 133
Reputation: 88
Single &
is the bitwise AND operator so 0 & 2 & 3
evaluates to 0 which makes your expression work like SelectedIndex.Equals(0)
. In order to check for multiple selected items, you have to check the .Selected
property of each item like:
CheckBoxList1.Items[0].Selected && CheckBoxList1.Items[2].Selected && CheckBoxList1.Items[3].Selected
Upvotes: -1
Reputation: 365
CheckBoxList1.SelectedIndex.Equals(0&2&3)
will evaluate to true for a index = 0 because 0&2&3
is zero. This is because in c# the & operator
is a bitwise AND.
These three combined with an binary AND operator (like you did) will result in 0.
Upvotes: 3