I have 68 booleans and there are going to be more. I need check all of them if they are false. If all are false then do something else (if only 1 of them is true) do something end if Is there an easier way then If bln1 = false and bln2 = false and bln3 = false....? Thanks
Here's an example in c# VS 2008 Code: bool[] flags = new bool[68]; for (int i = 0; i < flags.Length; i++) flags = false; flags[0] = true; // comment this line out and allfalse will be true, leave it in and allfalse will be false bool allfalse = !flags.Contains(true);
In the spirit of reusability I'd like to add one more way to solve the problem You can define a method that accepts infinite Boolean arguments: Code: static bool IsAllFalse(params bool[] booleans) { foreach (bool boolean in booleans) if (boolean) return false; return true; } Usage: Code: // Returns true (all values are false) IsAllFalse(false, false, false, false, false, false); // Returns false (one or more values are true) IsAllFalse(false, false, false, false, false, false, true);