Generic Lists and VB.NET

Introduction

Today you will learn about Generic Lists and how to use them productively in Visual Basic. NET

Generic Lists

According to MSDN, a Generic List "represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists". This is very vague and does not tell anyone about a Generic List's use and advantages. Let me try to explain it better: A Generic List is simply a list that you can make yourself. This list can be anything! An example of a generic List will be a student list. Do not think of the student list containing only the student's names, it can contain all methods and properties associated with a student object.

This means that you can create a Student class, supply some properties to it, and then make a list of Students, that will contain all the Student object's properties and methods.

An Example

As usual, I always have an example ready. Today we will create a Generic List of Students, then delve into this Generic List's properties and methods.

Design

Fire up Visual Studio 2012 and create a new VB.NET Windows Forms application. Once done, design your form to resemble Figure 1. This should give you a good indication of what you will be doing today.

Figure 1 - Our Design

Code

Adding the Class that will be the Generic List

Add a new class to the project and name it Students. Add the following into it:

Public Class Students
Private strStudentSurname As String
Private strStudentName As String
'Student Name Property
Public Property StudentName() As String
Get
Return strStudentName
End Get
Set
strStudentName = Value
End Set
End Property
'StudentSurname Property
Public Property StudentSurname() As String
Get
Return strStudentSurname
End Get
Set
strStudentSurname = Value
End Set
End Property
'ToString Method
Public Overrides Function ToString() As String
Return "Name: " & StudentName & " Surname: " & StudentSurname
End Function
'Equals Method
Public Overrides Function Equals(objTemp As Object) As Boolean
If objTemp Is Nothing Then Return False
Dim objAsStudent As Students = TryCast(objTemp, Students)
If objAsStudent Is Nothing Then
Return False
Else
Return Equals(objAsStudent)
End If
End Function
'GetHashCode Method
Public Overrides Function GetHashCode() As Integer
Return StudentSurname
End Function
'Equals Method
Public Overloads Function Equals(tempStudent As Students) As Boolean
If tempStudent Is Nothing Then
Return False
End If
Return (Me.StudentSurname.Equals(tempStudent.StudentSurname))
End Function
End Class

A lot happens here! Do not worry, it is not too complicated. Allow me to explain:

I added Properties for the Student Class. This gives us a way to get and set the Student name and Surname. Next, I added some methods. These methods enable us to check an object's existence inside the list, as well as to return the appropriate object when referenced.

Now, let's use this list of Students inside our form!

Import the Generic Lists namespace:

Imports System.Collections.Generic

Create the physical list:

'Create List Of Students
Private lstStudents As List(Of Students) = New List(Of Students)

This creates a list of students - with all its associated properties and methods.

Removing Specific Items from a Generic List

Add this code to remove an item from the list:

Private Sub btnRemoveAt_Click( sender As Object, e As EventArgs) Handles btnRemoveAt.Click
If lstStudents.Count > 2 Then
lstStudents.RemoveAt(1)
Else
MessageBox.Show("List Doesn't Have So Many Items Yet!")
End If
End Sub

Here, I simply made use of the Generic List's Contains method to determine if the supplied string is present or not. If it is, it will inform the user, else, it will also inform the user. You have to remember that it is case sensitive. If you want to make it case insensitive you'll have to override the Equals method again.

Displaying Items in a Generic List

Add the next code:

Private Sub btnShow_Click( sender As Object, e As EventArgs) Handles btnShow.Click
lstShowStudents.Items.Clear()
For Each currStudent As Students In lstStudents
lstShowStudents.Items.Add(currStudent)
Next
End Sub

This code simply loops through the Generic List and adds each item to the ListBox.

Conclusion

Thank you for reading my article. I hope that you have learned something new today and that you will now be able to do any task with a Generic List. Until next time, cheers!

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

On-demand Event
Event Date: February 12, 2015
The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …