Creating a List of Objects Using VB.NET

This article explains how to create list of objects in VB.NET.
  • 30304

Creating a List of Objects

The List class can be used to create any type including a class. In this article, we will see how to create a list of a class with several properties.

Example:

 We have a class named Author that has five pubic properties Name, Age, BookTitle, IsMVP and PublishedDate of type string, short, string, bool, and DateTime respectively.

Public Class Author

    Private m_name As String

    Private m_age As Short

    Private title As String

    Private mvp As Boolean

    Private pubdate As DateTime

    Public Sub New(name As String, age As Short, title As String, mvp As Boolean, pubdate As DateTime)

        Me.m_name = name

        Me.m_age = age

        Me.title = title

        Me.mvp = mvp

        Me.pubdate = pubdate

    End Sub

    Public Property Name() As String

        Get

            Return m_name

        End Get

        Set(value As String)

            m_name = value

        End Set

    End Property

    Public Property Age() As Short

        Get

            Return m_age

        End Get

        Set(value As Short)

            m_age = value

        End Set

    End Property

    Public Property BookTitle() As String

        Get

            Return title

        End Get

        Set(value As String)

            title = value

        End Set

    End Property

    Public Property IsMVP() As Boolean

        Get

            Return mvp

        End Get

        Set(value As Boolean)

            mvp = value

        End Set

    End Property

    Public Property PublishedDate() As DateTime

        Get

            Return pubdate

        End Get

        Set(value As DateTime)

            pubdate = value

        End Set

    End Property

End Class

The following code snippet creates a List of Author type.

Dim AuthorList As New List(Of Author)()

The following code snippet creates the Author objects and adds them to the List. And print all the items in list.

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

        Dim AuthorList As New List(Of Author)()

        AuthorList.Add(New Author("Mahesh Chand", 35, "A Prorammer's Guide to ADO.NET", True, New DateTime(2003, 7, 10)))

        AuthorList.Add(New Author("Neel Beniwal", 18, "Graphics Development with C#", False, New DateTime(2010, 2, 22)))

        AuthorList.Add(New Author("Praveen Kumar", 28, "Mastering WCF", True, New DateTime(2012, 1, 1)))

        AuthorList.Add(New Author("Mahesh Chand", 35, "Graphics Programming with GDI+", True, New DateTime(2008, 1, 20)))

        AuthorList.Add(New Author("Raj Kumar", 30, "Building Creative Systems", False, New DateTime(2011, 6, 3)))

        For Each author In AuthorList

            Console.WriteLine("Author: {0},{1},{2},{3},{4}", author.Name, author.Age, author.BookTitle, author.IsMVP, author.PublishedDate)

        Next

        Console.ReadLine()

    End Sub

End Module

Output:

Object-List-in-vb.net.jpg

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.