Boolean list

L

Luigi

Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is true
if *all* value in the list is true, and false if almost one of them is false?

Thanks in advance.
 
C

christoff

using System.Linq;
(...)
!aList.Any(b => !b)

Check the edge-case with the empty set.

Regards

--http://www.pajacyk.pl

public List<Boolean> GetValueFromList(List<Boolean> SetOfValue,Boolean
Value)
{

List<Boolean> Result = new List<Boolean>;

foreach (Boolean B in SetOfValue)
{
if (B)
{
Result.Add(B);
}
}

return Result;
}
 
C

Cor Ligthert[MVP]

In my idea then you can better use a byte and check that with normal boolean
or, and and xor.

However a little bit from the time when a cycle cost 1 second.

Cor
 
C

christoff

public List<Boolean> GetValueFromList(List<Boolean> SetOfValue,Boolean
Value)
{

    List<Boolean> Result = new List<Boolean>;

      foreach (Boolean B in SetOfValue)
       {
            if (B)
              {
                  Result.Add(B);
              }
       }

      return Result;

}

in addition :

Boolean Check(List<Boolean> Value)
{
Boolean Result = True;

foreach (Boolean B in Value)
{
if (!B)
{
Result = False;
break;
}

return Result;
}
}
 
L

Luigi

maciek kanski said:
using System.Linq;
(...)
!aList.Any(b => !b)

Check the edge-case with the empty set.

Unfortunately I can't use LINQ because I'm "fixed" with 2.0 framework.

Luigi
 
D

dunawayc

Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is true
if *all* value in the list is true, and false if almost one of them is false?

Thanks in advance.

You can use this line:

bool allTrue = listofbools.TrueForAll(delegate(bool value)
{ return value; });

Chris
 
O

Oleg Subachev

having a List said:
true
if *all* value in the list is true, and false if almost one of them is
false?

bool AllTrue(List<bool> list)
{
foreach (bool b in list)
if (!b)
return false;
return true;
}

Oleg Subachev
 
B

Ben Voigt [C++ MVP]

in addition :

Boolean Check(List<Boolean> Value)
{
Boolean Result = True;

foreach (Boolean B in Value)
{
if (!B)
{
Result = False;
break;
}

return Result;
}
}

I hope that yields some compiler warnings. In addition to some very
misguided attempt to maintain a single exit point and multiple misspelled
keywords, and using keywords as identifiers by varying only case, you've put
the unconditional (single exits are always unconditional) exit point inside
the loop.

Much better:

bool AreAllTrue(List<bool> booleans)
{
foreach (bool element in booleans)
{
if (!element) return false;
}
return true;
}
 
B

Ben Voigt [C++ MVP]

Luigi said:
Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is
true
if *all* value in the list is true, and false if almost one of them is
false?

Thanks in advance.

Really simple answer:

!booleans.Contains(false)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top