Generic List - Find

I'm very fond of using Generic Lists, but the FIND code is not exactly straight forward.

This is a series of examples of how to use the FIND method for a Generic List in VB.NET. The code is a complete runnable samle.

It's largly taken from the MSDN sample code but has been simplified some to be easier to read and use.

 

Related Articles

... and you 'll find more on the NET Development Menu

Imports System.Collections.Generic
 
Module Module1
    Private IDToFind As String = """"
    Public Books As New List(Of Book)
 
    Sub Main()
 
        FillList()
 
        ' Find a book by its ID.
        IDToFind = "1"
        Dim result As Book = Books.Find(AddressOf FindID)
        If result IsNot Nothing Then
            DisplayResult(result, "Find by ID: " & IDToFind)
        Else
            Console.WriteLine(vbCrLf & "Not found: " & IDToFind)
        End If
        Console.WriteLine()
 
        ' Find last book in collection that has a publish date before 2001.
        result = Books.FindLast(AddressOf PubBefore2001)
        If result IsNot Nothing Then
            DisplayResult(result, "Last book in collection published before 2001:")
        Else
            Console.WriteLine(vbCrLf & "Not found: " & IDToFind)
        End If
        Console.WriteLine()
 
        ' Find all computer books.
        Dim results As List(Of Book) = Books.FindAll(AddressOf FindComputer)
        If results.Count <> 0 Then
            DisplayResults(results, "All computer books:")
        Else
            Console.WriteLine(vbCrLf & "No books found.")
        End If
        Console.WriteLine()
 
 
        ' Find all books under $10.00.
        results = Books.FindAll(AddressOf FindUnderTen)
        If results.Count <> 0 Then
            DisplayResults(results, "Books under $10:")
        Else
            Console.WriteLine(vbCrLf & "No books found.")
        End If
        Console.WriteLine()
 
        ' Find index values.
        Console.WriteLine()
        Dim ndx As Integer = Books.FindIndex(AddressOf FindComputer)
        Console.WriteLine("Index of first computer book: " & ndx)
        ndx = Books.FindLastIndex(AddressOf FindComputer)
        Console.WriteLine("Index of last computer book: " & ndx)
 
        Dim mid As Integer = Books.Count / 2
        ndx = Books.FindIndex(mid, mid, AddressOf FindComputer)
        Console.WriteLine("Index of first computer book in the second half of the collection: " & ndx)
 
        ndx = Books.FindLastIndex(Books.Count - 1, mid, AddressOf FindComputer)
        Console.WriteLine("Index of last computer book in the second half of the collection: " & ndx)
 
 
    End Sub
 
 
 
    Private Sub FillList()
        Dim b As New Book
        b.ID = 1
        b.Author = "George Washington"
        b.Description = "GW Autobiography"
        b.Genre = "Autobiography"
        b.Price = 1.0
        b.Publish_date = "1/1/2012"
        b.Title = "George's Life"
        Books.Add(b)
 
        b = New Book
        b.ID = 1
        b.Author = "Scott Guthrie"
        b.Title = "VB.NET Explained"
        b.Description = "Everything about VB.NET"
        b.Genre = "Computer"
        b.Price = 2.0
        b.Publish_date = "1/1/2011"
        Books.Add(b)
 
    End Sub
 
    ' Predicate delegates for
    ' Find and FindAll methods.
    Private Function FindID(ByVal bk As BookAs Boolean
        If bk.ID = IDToFind Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function FindComputer(ByVal bk As BookAs Boolean
        If bk.Genre = "Computer" Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function FindUnderTen(ByVal bk As BookAs Boolean
        Dim tendollars As Double = 10.0
        If bk.Price < tendollars Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function PubBefore2001(ByVal bk As BookAs Boolean
        Dim year2001 As DateTime = New DateTime(2001, 1, 1)
        Return bk.Publish_date < year2001
    End Function
    Private Sub DisplayResult(ByVal result As BookByVal title As String)
        Console.WriteLine()
        Console.WriteLine(title)
        Console.WriteLine(vbLf & result.ID & vbTab & result.Author & _
                          vbTab & result.Title & vbTab & result.Genre & _
                          vbTab & result.Publish_date & vbTab & result.Price)
        Console.WriteLine()
    End Sub
    Private Sub DisplayResults(ByVal results As List(Of Book), ByVal title As String)
        Console.WriteLine()
        Console.WriteLine(title)
        For Each b As Book In results
            Console.Write(vbLf & b.ID & vbTab & b.Author & _
                              vbTab & b.Title & vbTab & b.Genre & _
                              vbTab & b.Publish_date & vbTab & b.Price)
        Next
        Console.WriteLine()
    End Sub
 
    Public Class Book
        Public ID As String
        Public Author As String
        Public Title As String
        Public Genre As String
        Public Price As Double
        Public Publish_date As DateTime
        Public Description As String
    End Class
 
 
End Module

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences