Introduction to the Generic List - Part II
August 29th, 2007
In Part I, I covered the basics of the List object as well as inserting and removing items and sorting items. In this part I’ll be covering searching, conversion, and enumeration functions available in the List object.
Searching
The List object offers several powerful search methods. The first is quite simple, Contains. It searches the List to see if it contains an object that matches the parameter as seen here:
If EmailAddresses.Contains(txtEmailAddress.text) Then
If you need to have more sophisticated existence searching you can use the Exists method. It takes a Predicate Delegate so you can write your own code for this purpose:
If EmailAddresses.Exists(AddressOf AddressLoaded) Then
.............
Private Function AddressLoaded(ByVal emailAddress As String) As Boolean
If emailAddress = txtEmailAddress.Text Then
Return True
Else
Return False
End If
End Function
IndexOf and LastIndexOf return the position of the specified item in the List should you want the index rather than the specific item itself.
Next we have the Find methods (Find, FindAll, FindLast, FindIndex, FindLastIndex) that also use Predicate Delegates. I covered these delegates in my previous article, How to Use the Action and Predicate Delegates, so I won’t cover them again here.
TrueForAll is another handy Predicate Delegate based search method that will tell you if a particular condition is true for all the items in the List:
If EmailAddresses.TrueForAll(AddressOf ValidateEmailAddress) Then
Our last searching method is BinarySearch. It uses a binary search algorithm, essentially the default Comparer or one you specify, to locate a specific item in the List. Note that the list should be sorted with the same Comparer first or else it won’t work correctly. This method is particularly handy for inserting new items into a particular place in the List. I don’t have a good example of this one but if there is demand for it I’ll include it in a future article dealing with comparers.
Conversion
The List also provides us with a number of ways to convert the data within it.
The AsReadOnly returns a ReadOnlyCollection:
Dim OptOutEmails As IList(Of String) = EmailAddresses.AsReadOnly
ConvertAll takes the content of the List and, using a Converter Delegate, creates a new list with a different type of item:
Dim Emails As New List(Of System.Net.Mail.MailMessage)
Emails = EmailAddresses.ConvertAll(New Converter(Of String, System.Net.Mail.MailMessage)(AddressOf ConvertToEmailMessage))
................
Private Function ConvertToEmailMessage(ByVal emailAddress As String) As System.Net.Mail.MailMessage
'email message building code would go here
End Function
We saw the CopyTo method used above when copying a ListBox’s SelectedItems collection to a List. This CopyTo works the same way:
Dim EmailsSent(EmailAddresses.Count - 1) As String EmailAddresses.CopyTo(EmailsSent)
Of course, it’s a lot easier to use the ToArray function to do the same thing:
Dim EmailsNotSent() As String EmailsNotSent = EmailAddresses.ToArray
The GetRange method allows you to extract a portion of the List as a list in its own right:
Dim ProcessNow As List(Of String) = EmailAddresses.GetRange(0, EmailAddresses.Count 2) . . .
Enumeration
As we discussed in the previous article on Actions and Predicates, ForEach uses Action Delegates to do something with each member of a List. For example:
Emails.ForEach(AddressOf SendEmail)
If you need more control, you can get the enumerator and manipulate it yourself with the GetEnumerator method.
I hope you’ve found these two overview articles helpful. If you did or didn’t please let me know by leaving a comment. It’s your feedback either through comments, contact me emails, or rating stars that help me chart the direction of this blog’s articles.
Entry Filed under: VB.NET Tutorials
Rate This Article:










Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed