views:

163

answers:

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;                           
}
+1  A: 

Intead of your code:

bool containsEmptyText = txtbox_and_message.Contains( t => t.Text ==String.Empty)
Oren A
+5  A: 
return true_or_false.Any(p => !p);
Homam
@Homam is it work with .net framework 2.0 ?
krunal shah
Unfortunately No, it needs 3.0 +
Homam
@krunal: If you have platform restrictions, you should put them in the question.
Billy ONeal
+1  A: 
using System.Linq;

then:

true_or_false.Contains(false);
rmx
@rmx 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
@krunal: What version of the .NET framework are you using?
BoltClock
You need to add a reference in your project to system.core dll
rmx
@BoltClock framework 2.0
krunal shah
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
rmx
+4  A: 

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
@Ben is it work with .net framework 2.0 ?
krunal shah
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
+2  A: 

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
`== false` and `== true` need to die. Otherwise a good answer +1.
Billy ONeal
Your second solution would perform better if you used `true_or_false.Any(x => !x)`
nasufara
@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
@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
thanks a lot guys. @Billy ive made the changes. and @ben, yes thats right. I've made the changes. thank you once again
Pavan
Where's the third solution?
Anthony Forloney
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
+1  A: 

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]);
}
BrunoLM
Why are you reaching for `List.Exists` instead of `Array.Exists`?
Ben Voigt