2

I am asked to develop an application where a member can select 1 plan, a combination of plans, all plans, or none.

plan1, plan2, plan3, .... plan12

I ran the truth table to find out how many possibilities there are, and it turned out to be 4096. (Ridiculous!)

My plan was to write an if statement for each possibility like this:

if (plan1.Checked == true && plan2.Checked == false && ... && plan12.Checked == false){
    // insert into into table test VALUES('Plan1')
}

and so on! Obviously, there must be a better and easier way than this. Any suggestions would help. Thank you all.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user3221917
  • 127
  • 3
  • 10
  • What happens if a plan is checked? Does a specific action take place for that plan? Or do specific things happen when certain plans are checked and others are not checked? – Andrew_CS Jul 09 '14 at 19:37
  • When a plan is checked, lets say plan1 is checked, text lets name it plan1 gets imported into a table in the database which will later be linked to another table to return a value. and if two plans are checked, plan1 and plan5, that would insert into the database 'plan1plan5' – user3221917 Jul 09 '14 at 19:40
  • THat would store plan1plan3, because on another table we have two columns, one column is called planIds and one plan Rate, basically plan1plan3 will be linked to that table and its plan Rate is plan1 * plan3 – user3221917 Jul 09 '14 at 19:43
  • I recommend looking into handling this using bitwise logic, this may be quite helpful: http://stackoverflow.com/a/8480/1316573 – Daniel Jul 09 '14 at 21:14

2 Answers2

1

If you used a CheckBoxList or similar component this would be one approach.

CheckBoxList checkBoxList = ....;  // Just an example here
// You would add the different project names into the CheckBoxList

String message = "";

for (int i = 0; i < checkBoxList.Items.Count; i++) // Look at every project name
{
    if (checkBoxList.Items[i].Selected) // See if it's selected
    {
        message += checkBoxList.Items[i].Text; // Add the name to the message
    }
}

Put message in DB; // <-- Store the message into your database here

Since you're only interested in selected items you wouldn't have to deal with non-selected items at all.

Note: There is probably a better/more efficient way of creating strings than this and you might be able to use lambda expressions. This is just showing a simple approach.

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
0

Andrew_CS answer is suitable and I prefer it, but here is another way:

Handle the checked event in the code behind of all checkboxes (use one event, but allow all checkboxes to be handled by it), and depending on the sender, then you can load the information you want.

It may be that you use a Select statement to load the relevant information for each checkbox during the checkbox checked event (might be a little easier to understand than a for loop!)