Net-informations.com
SiteMap  | About    

How to use VB.NET List

VB.NET List examples

vb.net 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

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()

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
        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




net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.