Hello everyone. I'm struggling to get my head around generics, so I decided to make an example.
I added a new class and typed :
As I understand, this will enable me to add Integers, Strings or whatever separately to this list, so this is what I did inside my FormCode:Public Class MyGenericList(Of T) Implements ICollection Implements IEnumerable Public InnerList As ArrayList = New ArrayList() Public Sub Add(ByVal val As T) InnerList.Add(val) End Sub Default Public ReadOnly Property Item(ByVal index As Integer) As T Get Return CType(InnerList(index), T) End Get End Property End Class
But nothing gets outputted to the Output window.Code:Public Class Form1 Private IntList As New MyGenericList(Of Integer)() Private StringList As New MyGenericList(Of String)() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load IntList.Add(0) IntList.Add(1) IntList.Add(2) IntList.Add(3) IntList.Add(4) StringList.Add("Zero") StringList.Add("One") StringList.Add("Two") StringList.Add("Three") StringList.Add("Four") End Sub Public Sub Display(Of T)(ByVal list As MyGenericList(Of T)) For Each obj As T In New List(Of T)() Console.WriteLine(obj.ToString) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim list As New MyGenericList(Of Integer) Display(Of Integer)(list) End Sub End Class
I'd just like to know how I can enumerate the items in my generic list. Can anyone help?![]()




Reply With Quote