September 10th, 2007
In a previous article I covered how to use the Action and Predicate Delegates. In this one we will be going over the fundamentals of using the Converter delegate.
This delegate is used to convert the members of a generic list from one type to another type. This technique can be very powerful when you need to do this kind of conversion since it handles all of the looping logic internally, making it much faster than a standard for…each loop.
Like the Action and Predicate delegates, the Converter delegate must be written in a specific way. A valid Converter is a function that has one parameter of the source type and a return value of the target type. Here are a few example functions declarations:
Public Function BuildMail(ByVal customerInfo As Customer) As Net.Mail.MailMessage .......... Public Function GetEmailAddresses(ByVal mail As Net.Mail.MailMessage) As String .......... Public Function BuildIDList(ByVal key As Guid) As Integer
Note that you can’t add parameters to this declaration. If you have additional values you would like to use in this routine you will need to have a module level variable or, if this is being done on a form, a control value.
Also, the ConvertAll method which uses this delegate is only available for the generic List and the untyped Array. It isn’t available for other generic types.
In this example we will be using the Converter delegate to convert a list of customer objects into a list of Net.Mail.MailMessage objects. We will assume that our customer objects have all of the required information and are stored in a module level list variable (’_customers’) and that our email message template is determined by a combobox selection on a form.
Here’s the code called from a button’s click event that will call the delegate, first broken down into individual commands and afterward as a single line call:
Private Sub btnCreateEmails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateEmails.Click Dim EmailsToSend As List(Of Net.Mail.MailMessage) EmailsToSend = customers.ConvertAll(Of Net.Mail.MailMessage)(AddressOf BuildMail) SendEmails(EmailsToSend) End Sub ...... Private Sub btnCreateEmails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateEmails.Click SendEmails(New List(Of Net.Mail.MailMessage) = _customers.ConvertAll(Of Net.Mail.MailMessage)(AddressOf BuildMail)) End Sub
You could write this function either way. I just wanted to show both.
Next, let’s take a look at the Converter delegate itself:
Public Function BuildMail(ByVal customerInfo As Customer) As Net.Mail.MailMessage Dim Email As New Net.Mail.MailMessage With Email .From = txtFromAddress.Text .To = customerInfo.EmailAddress .Subject = GetEmailSubject(cboEmailType.SelectedItem) .Body = GetEmailBody(cboEmailType.SelectedItem, customerInfo) End With Return Email End Function
In this function we see that an object from the original list is passed in and values are read from it to build the corresponding object for the new list. Also, we see values from controls being used to construct the new object as well.
I hope this description and example have given you some help in understanding the Converter delegate. Let me know if you have anything to add or if you have any questions by leaving a comment.
Entry Filed under: VB.NET Tutorials
Rate This Article: