views:

36

answers:

3

I have a list with a boolean value as one of the properties. I need to check if every one of these is true. Now normally LINQ would give a nice clean answer here, but as I am using .NET 2.0 I can't use that.

If there a nicer way to do this or am I going to have to do a for each loop and break out on finding a false?

Edit: Seems I could have been a bit clearer. I have an object in a list (eg List (Of MyObject)). There is a boolean property on this object called Processed.

I need to check that all objects in the list are processed.

So in LINQ I'd do: if (from o in list where o.processed = false select o).count = 0 then....

+1  A: 

ArrayList and List<T> has a bunch of methods that does pretty much what LINQ does. They have been there before LINQ.

Specifically, look at the Exists method. Unfortunately I dont know the VB syntax :)

leppie
How would I perform the above then? In LINQ I'd probably do a count of items that are false and check it's 0. What would I do using the `List<T>` methods?
themaninthesuitcase
+1  A: 

By using List<Of Boolean> you can use Contains() method:

If MyList.Contains(false) Then
  // at least one false
Else
  // all true
End If
Anax
Maybe I wasn't overly clear. I have a `List(Of MyObject>` and a property of `MyObject` is a boolean. I need to check that proprty, not a list of booleans.
themaninthesuitcase
A: 

You could write your own generic list implementation an add a property IsProcessedCompletely:

 Public Class My_Class
        Public Property IsProcessed() As Boolean
            Get
            End Get
            Set(ByVal value As Boolean)
            End Set
        End Property
    End Class

    Public Class My_List
        Inherits List(Of My_Class)

        Public ReadOnly Property IsProcessedCompletely() As Boolean
            Get
                Dim enumerator As List(Of My_Class).Enumerator = MyBase.GetEnumerator
                While enumerator.MoveNext
                    If Not enumerator.Current.IsProcessed Then
                        Return False
                    End If
                End While
                Return True
            End Get
        End Property
    End Class
Tim Schmelter
I sort of went with this, as it is only ever used in 1 place I just wrote a quick loop to check for it in a small function, didn't inherit. This is exactly what I was trying not to do but seems to be the only way.
themaninthesuitcase