How to use VB.NET List

VB.NET List examples list example

Generic lists were introduced with .Net 2.0 and are the way to go. List is a generic implementation of ArrayList. List can store only one type of objects, that type supplied as its generic parameter. List class is a collection and defined in the System.Collections.Generic namespace and it provides the methods and properties like other Collection classes such as add, insert, remove, search etc.

To use List(Of T), however, you have to understand how to implement the many methods that the .NET Framework provides

List(Of T)

The parameter T is the type of elements in the list.

How to add items in List ?

Add integer values in the List

Add string values in the List

How to count number of items exists in a List ?

List.Count property gives you the number of items exists in List

How to retrieve items from List ?

You can retrieve items from List collection by using for loops.

foreach loop

for loop

How to insert an item in the List ?

You can use insert(index,item) method to insert an in the specified index.

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.

How to check if an item exist in the List ?

You can use List.Contains() method to check an item exists in the List.

How to copy an Array to a List ?

Finally clear method remove all the items from List collection.

Difference between list and dictionary in VB.Net

In VB.Net applications both lists and dictionaries are used to store collections of data. List (ICollection ) is simply a set of items and Dictionary(IDictionary) is a set of key-value pairs. The essential difference therefore is in how the containers are indexed data in your VB.Net application. If you want to know more details.....What is the difference between list and dictionary in VB.Net?

The following VB.Net program shows the implementation of the above functionalities in List collection.

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

        'insert an item in the list
        numbers.Insert(1, "Zero")

        'retrieve items using foreach loop
        For Each number As String In numbers

        'remove an item from list

        'retrieve items using for loop
        For i As Integer = 0 To numbers.Count - 1

        If numbers.Contains("Two") Then
            MessageBox.Show("Number two exist in the list")
            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

        'call clear method


    End Sub
End Class (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About