Generics.vb
' ' Generics example by Josh Santomieri ' Santomieri Systems - 08/25/2006 ' Imports Microsoft.VisualBasic Imports System.Collections '''''' Represents a list of type T ''' '''The type of information that will be going into the list. Public Class MyList(Of T) Implements IEnumerable Private _list As ArrayList '''''' Creates a new MyList. ''' Public Sub New() _list = New ArrayList() End Sub '''''' Add an item to the list. ''' ''' The item to add to the list. Public Sub Add(ByVal item As T) If item IsNot Nothing Then Me._list.Add(item) End If End Sub '''''' Gets or sets the item at index i. ''' ''' The item in the list to get or set. Default Public Property Item(ByVal i As Integer) As T Get Return CType(Me._list(i), T) End Get Set(ByVal value As T) Me._list(i) = value End Set End Property '''''' Gets the count of items in the list. ''' Public ReadOnly Property Count() Get Return Me._list.Count End Get End Property '''''' Gets the enumerator for the list. ''' Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return Me._list.GetEnumerator() End Function End Class Public Class MyProgram Public Shared Sub Main(ByVal args As String()) Dim stringList As New MyList(Of String) Dim intList As New MyList(Of Integer) ' Add strings to the string list. stringList.Add("Test") stringList.Add("Test1") stringList.Add("Test2") ' Add integers to the integer list. intList.Add(100) intList.Add(200) intList.Add(300) ' loop through the string list For Each s As String In stringList Console.WriteLine("String Value: " & s) Next ' loop through the integer list For Each i As Integer In intList Console.WriteLine("Integer Value: " & i) Next End Sub End Class