How to use VB.NET List
Generic lists were introduced in .NET 2.0 and have become the preferred approach for managing collections. The List class is a generic implementation of the ArrayList, enabling storage of a specific type of objects, specified as its generic parameter. It belongs to the System.Collections.Generic namespace and offers a range of methods and properties similar to other collection classes, including add, insert, remove, and search functionalities.
To effectively utilize List(Of T), it is crucial to comprehend the implementation of numerous methods provided by the .NET Framework.
List(Of T)
The parameter T is the type of elements in the list.
Important functions in VB.Net List
How to add items in List ?
Add integer values in the List:
Dim iList As New List(Of Integer)()
iList.Add(2)
iList.Add(3)
iList.Add(5)
iList.Add(7)
Add string values in the List:
Dim numbers As New List(Of String)()
numbers.Add("One")
numbers.Add("Two")
numbers.Add("Three")
How to count number of items exists in a List ?
List.Count property gives you the number of items exists in List
numbers.Count
How to retrieve items from List ?
You can retrieve items from List collection by using for loops.
foreach loop:
For Each number As String In numbers
MessageBox.Show(number)
Next
for loop:
For i As Integer = 0 To numbers.Count - 1
MessageBox.Show(numbers(i))
Next
How to insert an item in the List ?
You can use insert(index,item) method to insert an in the specified index.
numbers.Insert(1, "zero")
In the above code the number "zero" is inserted in the index position 1.
How to remove an item from List collection?
List.Remove() can use to remove item from List.
numbers.Remove("zero");
How to check if an item exist in the List ?
You can use List.Contains() method to check an item exists in the List.
if (numbers.Contains("Two"))
{
MessageBox.Show("Number Two exist in the list");
}
How to copy an Array to a List ?
Dim strArr As String() = New String(2) {}
strArr(0) = "Red"
strArr(1) = "Blue"
strArr(2) = "Green"
//here to copy array to List
Dim arrlist As New List(Of String)(strArr)
Finally clear method remove all the items from List collection.
arrlist.Clear()
The provided VB.NET source code showcases a selection of frequently utilized functions.
Full Source VB.NET
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbers As New List(Of String)()
'add items in a List collection
numbers.Add("One")
numbers.Add("Two")
numbers.Add("Three")
'insert an item in the list
numbers.Insert(1, "Zero")
'retrieve items using foreach loop
For Each number As String In numbers
MessageBox.Show(number)
Next
'remove an item from list
numbers.Remove("Zero")
'retrieve items using for loop
For i As Integer = 0 To numbers.Count - 1
MessageBox.Show(numbers(i))
Next
If numbers.Contains("Two") Then
MessageBox.Show("Number two exist in the list")
Else
MessageBox.Show("Not exist")
End If
'copy array to list
Dim strArr As String() = New String(2) {}
strArr(0) = "Red"
strArr(1) = "Blue"
strArr(2) = "Green"
Dim arrlist As New List(Of String)(strArr)
For Each str As String In strArr
MessageBox.Show(str)
Next
'call clear method
arrlist.Clear()
MessageBox.Show(arrlist.Count.ToString())
End Sub
End Class