Question

I have an issue where I need to create some buttons depending on some boolean values.

If button1 = true I should create button1, if button2 = true I should create button2 and if button3 = true I should create button3.

So there can be 8 combinations:

1 - button1, button2, button3 (button1 = true, button2 = true, button3 = true)
2 - button1, button2 (button1 = true, button2 = true, button3 = false)
3 - button1, button3
4 - button2, button3
5 - button2, button1
6 - button1
7 - button2
8 - button3

My issue is how to find the correct combination out of the 8.

Was it helpful?

Solution 2

Not sure what the question really is: seems like programming 101 (or programming for dummies page 2). The obvious approach:

if (button1) { createButton1(); }
if (button2) { createButton2(); }
if (button3) { createButton3(); }

If the combinations are a bit more complex, you might need separate statements:

if (button1 && button2 && button3) {
    // create required buttons for case 1
} else if (button1 && button2) {
    // create required buttons for case 2
}
...

The order of the cases in the questions are ok - you need most specific to least specific (if "just button1" was first, it would "steal" all the other cases relying on button 1.)

Another approach is to encode the booleans into an int and use a switch. This is probably a bit more extensible if you might need to add complex conditions for buttons 4 5 and 6

int choice = button1 ? 1 : 0;
choice += button2 ? 2 : 0;
choice += button3 ? 4 : 0;
switch(choice) {
    case 0: break; // no buttons
    case 1: // just button1
    case 2: // just button2
    case 3: // button1 and button2
    ...
}

OTHER TIPS

If button1 = true I should create button1, if button2 = true I should create button2 and if > button3 = true I should create button3.

you seem to have written the complete pseudocode already. try three if statements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top