The Useful Generic List in VB.NET

Example Code and Explanations of the ForEach, FindAll, and Sort Methods

The concept of "generic objects" in VB.NET is introduced in the article, Generics! Cleaner Data - Faster Code!. Generics extend the power and flexibility of VB.NET in a lot of areas, but you get a bigger performance benefit and more programming options in the generic List object -- List(Of T) -- than with any other. Check out that article to see a performance comparison between ArrayList and the generic List(Of T) doing the same programming task.

To use List(Of T), however, you have to understand how to implement the many methods that the .NET Framework provides. That's what this article is about. I've programmed three examples -- using ForEach, FindAll, and Sort -- to demonstrate how the generic List class works.

As explained in the first article linked above, step 1 is to create a generic List. You can get the data in a lot of ways, but the simplest way is to simply Add it. In this article, I'm going to write code to classify my beer and wine collection! So here's the code to create the collection:

First, I need an object that will represent a bottle from my collection. The code for this is totally standard. Here's my object. (In a Windows Forms application, the Form class has to be first in a file or the Visual Studio designer won't work correctly, so put this at the end.)

 Public Class Bottle
    Public Brand As String
    Public Name As String
    Public Category As String
    Public Size As Decimal
    Public Sub New( _
       ByVal m_Brand As String, _
       ByVal m_Name As String, _
       ByVal m_Category As String, _
       ByVal m_Size As Decimal)
       Brand = m_Brand
       Name = m_Name
       Category = m_Category
       Size = m_Size
    End Sub
 End Class 

To build the collection, I Add the items. I put this in the Form Load event.

 Dim Cabinet As List(Of Bottle) = _
    "New List(Of Bottle)
    Cabinet.Add(New Bottle( _
       "Castle Creek", _
       "Uintah Blanc", _
       "Wine", 750))
    Cabinet.Add(New Bottle( _
       "Zion Canyon Brewing Company", _
       "Springdale Amber Ale", _
       "Beer", 355))
    Cabinet.Add(New Bottle( _
       "Spanish Valley Vineyards", _
       "Syrah", _
       "Wine", 750))
    Cabinet.Add(New Bottle( _
       "Wasatch Beers", _
       "Polygamy Porter", _
       "Beer", 355))
    Cabinet.Add(New Bottle( _
       "Squatters Beer", _
       "Provo Girl Pilsner", _
       "Beer", 355)) 

All this was standard code in VB.NET 1.0. But note that by defining our own Bottle object, we get the benefits of multiple types in the same collection (in this case, both String and Decimal) and efficient, type safe "late binding".

On the next page, we get to the heart of the matter ... the ForEach, FindAll, and Sort methods.

ForEach Example

The fun starts when we use the methods. To begin with, let's implement the familiar ForEach method. The Microsoft documentation includes this usage syntax definition:

 Dim instance As List
 Dim action As Action(Of T)

Microsoft further defines action as "delegate to a method that performs an action on the object passed to it. The elements of the current List(T) are individually passed to the Action(T) delegate"

If you need more on delegates, you might want to try the About Visual Basic article, Using Delegates in Visual Basic .NET for Runtime Flexibility.

The first thing you need to code is the method that will be delegated. Misunderstanding this one key point is the source of most of the confusion of VB.NET students. This function or subroutine is where all of the customized coding for the "Of <T>" type objects is done. When you get this right, you're essentially done. In this first example, it's really simple. An entire instance of the Bottle is passed and the subroutine selects anything needed out of it. Coding the ForEach itself is simple too. Just fill in the address of the delegate using the AddressOf method.

 Sub displayBottle(ByVal b As Bottle)
    ResultList.Items.Add( _
       b.Brand & " - " & _
       b.Name & " - " & _
       b.Category & " - " & _
 End Sub
 Private Sub ForEachButton_Click( ...
    ResultList.Items.Add("For Each Example")
    Cabinet.ForEach(AddressOf displayBottle)
 End Sub 

FindAll Example

FindAll is a little more complicated. The Microsoft documentation for FindAll looks like this:

 Dim instance As List
 Dim match As Predicate(Of T)
 Dim returnValue As List(Of T)
 returnValue = instance.FindAll(match) 

This syntax includes a new element, Predicate(Of T). According to Microsoft, this will represent the method "that defines a set of criteria and determines whether the specified object meets those criteria." In other words, you can create any code that will find something in the list.

I coded my Predicate(Of T) to find anything in the "Beer" Category:

Instead of calling the delegate code for each item in the list, FindAll returns an entire List(T) containing only the matches that result from your Predicate(Of T). It's up to your code to both define this second List(T) and do something with it. My code just adds the items to a ListBox.

 Private Sub FindAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindAllButton.Click
    ResultList.Items.Add("FindAll Example")
    Dim sublist As List(Of Bottle)
    sublist = Cabinet.FindAll(AddressOf findBeer)
    For Each r As Bottle In sublist
       ResultList.Items.Add( _
          r.Brand & " - " & _
          r.Name & " - " & _
          r.Category & " - " & _
 End Sub
 Function findBeer(ByVal b As Bottle) _
    As Boolean
    If (b.Category = "Beer") Then
       Return True
       Return False
    End If
 End Function 

Sort Example

The final method this article examines is Sort. Again, Microsoft uses some terminology you might not be familiar with. There are actually four different overloads of the Sort method:

  • Sort()
  • Sort(IComparer(T))
  • Sort(Comparison(T))
  • Sort(Int32, Int32, IComparer(T))

This lets you use sort methods defined in the .NET Framework for the list, code your own, use a system defined comparison for the type, or sort part of the collection using a starting position and count parameter. In this example, since I use the syntax ...


... to actually perform the sort, I'm using the third overload. I've coded another delegate to my own comparer. Since I want to sort by my Name, I pull just that value out of each instance of the Bottle object that is passed and use the Sort(Comparison<(Of <(T>)>)). The Sort method actually rearranges the original List(T). So that's what is processed after the method is executed.

 Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
    ResultList.Items.Add("Sort Example")
    Cabinet.Sort(AddressOf sortCabinet)
    For Each r As Bottle In Cabinet
       ResultList.Items.Add( _
          r.Name & " - " & _
          r.Brand & " - " & _
          r.Category & " - " & _
 End Sub
 Private Shared Function sortCabinet( _
    ByVal x As Bottle, ByVal y As Bottle) As Integer
    Return x.Name.CompareTo(y.Name)
 End Function 

These methods were selected to demonstrate the major ways that the Framework methods in List(T) are actually coded. There's a whole raft of other methods, however. That's what makes List(T) so useful!

If you would like to experiment with generic lists yourself, Click Here to download of the completed system above to help you get started.