Tuesday, July 17, 2012

4 Checkboxes – Some tricky issue - solved using Ternary operator

4 Checkboxes – Some tricky issue

Today, almost more than 4 hrs I'd spent on this issue. Do you want to know what is that? Here is that. I have 4 checkbox elements and I have to do the following conditions
Condition 1) Any one check box has to selected
Condition 2) If any of the first 2 check boxes are selected, my code should throw an exception when the 3rd check box is selected
Condition 3) vice versa for Condition 2
A picture is worth of 1000 words. Here is the actual requirement.issue
Hope you got the actual picture. Now here comes the actual twist. You can do that with simple if conditions, but i thought of implementing this using territory operators using a single if condition. And the condition goes like this..

if(((cbRepair1.Checked || cbCalibration1.Checked ) ? 
    (cbBoth1.Checked) ? false : true : 
    (cbBoth1.Checked || cbOnLine1.Checked) ? true : false)==false){
    throw new Exception("Duplicate selection ..");

No comments: