ARTICLE

how to work with generic classes in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language July 08, 2010
To work with a collection of objects you use generic collection.
 
Reader Level:

Whenever you need to work with a collection of objects you use generic collection. .NET framework providing some generic collection such as the ones define by the List(), SortedList(), Stack(), Queue() classes.

So many times you need to use generics to define your own generic collection to add some functionality which is not provided in .NET Framework generic collections. So, this article show you how to do that.
 
In this example first creates a CustomList() class that can store Product types, add four product object to the list, and display these objects in a dialog box.

EXAMPLE CODE
Imports System.Windows.Forms
Module Module1
    Public Class CustomList(Of T)
       
Private List As New List(Of T)

        Public Sub Add(ByVal item As T)
            List.Add(item)
        End Sub

        Default
Public Property Item(ByVal indax As Integer) As T
            Get
                Return
List(indax)
            End Get
            Set
(ByVal value As T)
                List(indax) = value
            End Set
        End
Property

        Public
ReadOnly Property Count() As Integer
            Get
                Return
List.Count
            End Get
        End
Property

        Public
Overrides Function ToString() As String
            Dim
listString As String = ""
            For i As Integer = 0 To List.Count - 1
                listString &= List(i).ToString & vbCrLf
            Next
            Return
listString
        End Function
    End
Class

    Sub
Main()
        Dim list As New CustomList(Of Integer)
        Dim a1 As Integer = 23
        Dim a2 As Integer = 71
        Dim a3 As Integer = 10
        Dim a4 As Integer = 50
        list.Add(a1)
        list.Add(a2)
        list.Add(a3)
        list.Add(a4)
        MessageBox.Show(list.ToString, "List of Integers")
    End Sub
End
Module

OUTPUT

GENER.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor