Hi,
I was wondering if my way of checking which combination of checkbox' that are checked are extremly unefficient? I have previously done it like this:
Any improving suggestion on how to iterate through checkboxes to find which combination that is checked?
//If no heuristics is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If only voyageCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If only costEffectivenessCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If only accWaitingCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If only accWaitingCheckBox is active // if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 1) { }
//If voyageCutCheckBox and costEffectivenessCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If voyageCutCheckBox and accWaitingCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If voyageCutCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 1) { }
//If costEffectivenessCheckBox and accWaitingCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If costEffectivenessCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 1) { i }
//If accWaitingCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 1) { }
//If voyageCutCheckBox, costEffectivenessCheckBox and accWaitingCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 0) { }
//If voyageCutCheckBox, costEffectivenessCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 0) if (checkboxvalues.ballastCutCheckBox == 1) { }
//If voyageCutCheckBox, accWaitingCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 0) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 1) { }
//If voyageCutCheckBox, costEffectivenessCheckBox, accWaitingCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 0) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 1) { } //If voyageCutCheckBox, costEffectivenessCheckBox, accWaitingCheckBox and ballastCutCheckBox is active if (checkboxvalues.voyageCutCheckBox == 1) if (checkboxvalues.costEffectivenessCheckBox == 1) if (checkboxvalues.accWaitingCheckBox == 1) if (checkboxvalues.ballastCutCheckBox == 1) { }
|
Sam HobbsPosted Dec 18, 2010, 5:25 AM
theLizardPosted Dec 18, 2010, 5:09 AM
Sam what you have tested may have worked for you but you have not shown what or how it worked, one thing is for sure, you can not use an integer to to represent a binary value having a leading zero, you cant even use a float or double it must be a string type Unicode :) or otherwise...
Suthish NairPosted Dec 18, 2010, 1:20 AM
Sam HobbsPosted Dec 18, 2010, 12:29 AM
theLizardPosted Dec 17, 2010, 11:32 PM
Sam HobbsPosted Dec 17, 2010, 11:02 PM
I think theLizard's suggestion is a good start. What you can do is to convert the various boolean values into one integer value, then use an array of delegates to call the function corresponding to the combination. The following will show what I mean.
Define a delegate, as in: Then declare a List of the delegates: I assume you already have a function for each combination of boolean values; the following are skeleton samples, but you can probbly use what you already have; maybe with a slight modification. Then in your initialization code, such as in the form's constructor, build a List of functions.
Again, note that that is done only once during execution of the program. Then each time you need to find which combination that is checked, just do something such as: And that will be very efficient. Note that I am not sure what order that the checkboxes need to be in (voyageCutCheckBox .... ballastCutCheckBox or ballastCutCheckBox .... voyageCutCheckBox ); you need to figure that out.
AmundPosted Dec 17, 2010, 6:52 AM
theLizardPosted Dec 17, 2010, 5:13 AM
concatenate the values of each of the four checkboxes into a string eg 0100, 1000, 0010, this may need to be done in a couple of steps eg
string values = getbinary(checkbox1.value) + getbinary(checkbox2.value) ...
string getbinary(bool value)
{
return(value==true? "1" : "0");
}
then you can switch the values eg,
switch(values)
{
case "1000":
//do your stuff
break;
case "0100":
//do your stuff
break;
case "0010":
//do your stuff
break;
}
I don't think there would be any other way. you could also set the check state in a similar way.
Good luck.
Manikavelu VelayuthamPosted Dec 17, 2010, 4:45 AM
You should follow some alogrithmic way to implement it. In that way, you can avoid the repeated checks.
Just try to find an efficient algorithm to implement your requirement.
AmundPosted Dec 17, 2010, 4:40 AM
OK, but if i store the values in local variables I still need 16 entries to iterate through all 16 combinations possible with 4 checkboxes? If i had 10 checkboxes I would still need to list all 100 possible combination outcomes? Or am I missing something in your answer? :)
Manikavelu VelayuthamPosted Dec 17, 2010, 4:14 AM
int a = checkboxvalues.voyageCutCheckBox
int b = checkboxvalues.costEffectivenessCheckBox
int c = checkboxvalues.accWaitingCheckBox
int d = checkboxvalues.ballastCutCheckBox
Finally use the above variables in your condition.
In your code, each and everytime you are getting the value of checkbox. If you store the values in the local variable, value will be reference only once from the check box.