6

How to check the array true_or_false containing a value of false?

bool[] true_or_false = new bool[10];

for (int i = 0; i < txtbox_and_message.Length; i++)
{
  bool bStatus = true;
  if (txtbox_and_message[i] == "")
  {
    bStatus = false;
  }
  true_or_false[i] = bStatus;                           
}
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
krunal shah
  • 15,347
  • 24
  • 90
  • 136
  • What is the loop doing? Looks like it checks for blank text (repeatedly) and sets *all* elements either true, or false if the string is blank? – PaulG Oct 09 '10 at 22:33
  • 1
    Shouldn't `bStatus` depend somehow on `i`? This is why PaulG correctly says that all elements are the same. – Ben Voigt Oct 09 '10 at 22:37
  • possible duplicate of [Returning booleans in a C# method](http://stackoverflow.com/questions/3852078/returning-booleans-in-a-c-method) – Ahmad Mageed Oct 09 '10 at 22:38
  • Are you missing txtStarter=txtbox_and_message[i]; ? Or what Ben said? Your code obviously contains errors. – Oren A Oct 09 '10 at 22:41
  • I have modified my question.. – krunal shah Oct 09 '10 at 22:46
  • It still doesnt make sense! If you're iterating the length of txtbox_and_message, then txtbox_and_message[i] will never be "" – PaulG Oct 09 '10 at 22:52
  • @PaulG: Not so. The `.Length` is the number of strings in the array, not the length of any particular string. – Ben Voigt Oct 09 '10 at 23:05
  • @Ben, ah yes - makes sense now! I thought txtbox_and_message was a single string rather than an array, and krunal was trying to check characters in the string. Thanks – PaulG Oct 09 '10 at 23:08

6 Answers6

15

If they are not all true, then at least one is false.

Therefore:

!true_or_false.All(x => x)

Docu: http://msdn.microsoft.com/en-us/library/bb548541.aspx

EDIT: .NET 2.0 version, as requested:

!Array.TrueForAll(true_or_false, delegate (bool x) { return x; })

or

Array.Exists(true_or_false, delegate (bool x) { return !x; })

NOTE: I've been staying away from the nonsensical code that sets true_or_false, but it could be that what you want is:

int emptyBox = Array.FindIndex(txtbox_and_message, string.IsNullOrEmpty);

which will give you -1 if all the strings are non-empty, or the index of the failing string otherwise.

Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
  • No, the extension method syntax and lambda notation for the predicate both need C# 3, but a .NET 2 version does exist (see edit). – Ben Voigt Oct 09 '10 at 23:01
  • You do not really need delegates (anonymous function or lambda) for this. You can do `Array.Contains(true_or_false, false)` which comes very close to the title (check if array contains false). That is on .NET 2.0. When you have LINQ, you can also use the syntax `true_or_false.Contains(false)`. _EDIT:_ WRONG! In .NET 2.0, you have to do `Array.IndexOf(true_or_false, false) != -1` instead. – Jeppe Stig Nielsen Apr 13 '18 at 17:20
  • @JeppeStigNielsen: Sure but my answer shows that the index can be found without having to prepare the array of booleans in advance, which I considered to be better than searching the array of booleans. – Ben Voigt Apr 13 '18 at 18:56
11
return true_or_false.Any(p => !p);
Homam
  • 21,556
  • 28
  • 104
  • 181
8
using System.Linq;

then:

true_or_false.Contains(false);
Nobody
  • 4,541
  • 5
  • 30
  • 62
  • I am getting this error after adding System.Linq. The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) – krunal shah Oct 09 '10 at 22:34
  • You need to add a reference in your project to system.core dll – Nobody Oct 09 '10 at 22:38
  • LINQ comes with .NET 3.5 and VS 2008 so you will need to use an iterative solution such as Pavan or BrunoLM's answers – Nobody Oct 09 '10 at 22:52
2

Intead of your code:

bool containsEmptyText = txtbox_and_message.Contains( t => t.Text ==String.Empty)
Oren A
  • 5,686
  • 5
  • 39
  • 59
2

There are a couple of solutions:

Solution 1: do a for loop after that for loop to check if the true_or_false contains false like this:

if you want to achieve this without fancy tricks, and you want to program the code yourself you can do this:

bool containsFalse = false;
for(int j = 0; j < true_or_false.Length; j++)
{
   //if the current element the array is equals to false, then containsFalse is true,
   //then exit for loop
   if(true_or_false[j] == false){
       containsFalse = true;
       break;
   }
}

if(containsFalse) {
  //your true_or_false array contains a false then.
}

Solution 2:

!true_or_false.All(x => x);

PK

Pavan
  • 15,460
  • 8
  • 56
  • 99
  • 4
    `== false` and `== true` need to die. Otherwise a good answer +1. – Billy ONeal Oct 09 '10 at 23:42
  • 3
    Your second solution would perform better if you used `true_or_false.Any(x => !x)` –  Oct 09 '10 at 23:54
  • @nasufara: On what evidence do you base this claim? Both `Any` and `All` are short-circuiting. If anything, the need to call the `!` (boolean NOT) operator only once should make `All` faster, but the difference is almost certainly too small to measure. – Ben Voigt Oct 10 '10 at 00:37
  • 1
    @Pavan: you can make method #1 faster by adding `break;` inside the if, as you don't need to keep looking after you found the first false. – Ben Voigt Oct 10 '10 at 00:40
  • thanks a lot guys. @Billy ive made the changes. and @ben, yes thats right. I've made the changes. thank you once again – Pavan Oct 10 '10 at 12:42
  • there was a platform restriction with the person asking the question so i didnt put the third solution up. Ive made the changes though. thanks – Pavan Oct 10 '10 at 12:49
1

If on .NET3.5+ you can use System.Linq, and then check using Any:

// if it contains any false element it will return true
true_or_false.Any(x => !x); // !false == true

If you can't use Linq, then you have other choises:

Using Array.Exists static method: (as Ben mentioned)

Array.Exists(true_or_false, x => !x);

Using List.Exists (you would have to convert the array to a list to access this method)

true_or_falseList.Exists(x => !x);

Or you will need to iterate through the array.

foreach (bool b in true_or_false)
{
    if (!b) return true; // if b is false return true (it contains a 'false' element)
}
return false; // didn't find a 'false' element

Related


And optimizing your code:

bool[] true_or_false = new bool[10];

for (int i = 0; i < txtbox_and_message.Length; i++)
{
    true_or_false[i] = !String.IsNullOrEmpty(txtbox_and_message[i]);
}
Community
  • 1
  • 1
BrunoLM
  • 88,362
  • 76
  • 272
  • 427