in

ASP.NET Weblogs

Carl Franklin

.NET Wonk

Some REAL Generics in VB.NET Whidbey

So, I fired up Whidbey to write some code that uses a generic collection. Everything I could find on the web was just something like this

    Dim col As New List(Of String)

    col.Add(”Hi”)

    Msgbox(col(0).ToLower)

This code doesn't say what List is, so I went to Amanda's PPT file from the PDC  (choose TLS300) and the slide indicates that List is a custom class that looks like a collection.... kind of.

That stumped me. I knew I didn't have to write my own generic ArrayList class with (Of T), and after a bit of searching I found the Generic namespace. It has generic versions of the Comparer, Dictionary, KeyValuePair, List, Queue, SortedDictionary, and Stack classes. (Note, these may change in the future, so just take this post as an educational rant)

So, here's some code that I wrote to create a generic list of a class called Message, which contains a name, email address, and the message text. I was able to bind this list to the Listbox with no problem, and it was a piece of cake to work with.

Here's the Message class complete with XML Comments (the characters of which also may change according to Paul Vick):

Public Class Message

    Private _sendername, _senderemail, _text As String

    Public Property SenderName() As String

        Get

            Return _sendername

        End Get

        Set(ByVal Value As String)

            _sendername = Value

        End Set

    End Property

    Public Property SenderEmail() As String

        Get

            Return _senderemail

        End Get

        Set(ByVal Value As String)

            _senderemail = Value

        End Set

    End Property

    Public Property Text() As String

        Get

            Return _text

        End Get

        Set(ByVal Value As String)

            _text = Value

        End Set

    End Property

End Class

And here is the Windows Form code. There is only a single Listbox on the form.

This line goes at the class level:

    Private col As New Generic.List(Of Message)

And this code goes in Form_Load:

    '-- Add a new message

    col.Add(New Message)

    col(0).SenderName = "Carl Franklin"

    col(0).SenderEmail = "carl@franklins.net"

    col(0).Text = "This is message one"

    '-- Add another new message

    col.Add(New Message)

    col(1).SenderName = "Jay Franklin"

    col(1).SenderEmail = "jay@franklins.net"

    col(1).Text = "This is message two"

    '-- Bind Listbox

    With Listbox1

        .DisplayMember = "SenderName"

        .ValueMember = "SenderEmail"

        .DataSource = col

    End With

The names Carl Franklin and Jay Franklin show up in the list. When you double-click them, this code executes:

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

        Dim msg As Message = CType(ListBox1.SelectedItem, Message)

        MsgBox("Message from " & msg.SenderEmail & vbCrLf & "Text: " & msg.Text)

    End Sub

So you can see how this is cool, right? First of all, you get the late-binding feature you always wanted out of the ArrayList (or List) and secondly, the compiler generates more efficient and type-safe code. It's a win-win.

Comments

 

TrackBack said:

November 14, 2003 1:02 PM
 

bob said:

Remember to add:

Imports System.Collections.Generic

To the top of your code to use lists.  Had me stumbled for a while...

Bob

May 12, 2009 11:08 AM

Leave a Comment

(required)  
(optional)
(required)  
Add