-
December 30th, 2008, 02:32 AM
#1
[RESOLVED] [2005] Generics (Of T)
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 :
Code:
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
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 Form
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
But nothing gets outputted to the Output window.
I'd just like to know how I can enumerate the items in my generic list. Can anyone help?
-
December 30th, 2008, 04:32 AM
#2
Re: [2005] Generics (Of T)
Don't worry, I fixed it myself. This is what I did:
Generic Class:
Code:
Public Class MyGenericList(Of T)
Inherits CollectionBase
Implements ICollection
Public Function Add(ByVal value As T) ' As T
Return List.Add(value)
End Function
Public Sub Remove(ByVal value As T)
List.Remove(value)
End Sub
Public ReadOnly Property Item(ByVal index As Integer) As T
Get
Return CType(List.Item(index), T)
End Get
End Property
End Class
Form:
Code:
Public Class Form1
Private IntList As New MyGenericList(Of Integer)()
Private StringList As New MyGenericList(Of String)()
Private ButClicked As Integer
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))
Select Case ButClicked
Case 0
For Each obj As T In IntList
Console.WriteLine(obj.ToString)
Next
Case 1
For Each obj As T In StringList
Console.WriteLine(obj.ToString)
Next
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ButClicked = 0
Dim list As MyGenericList(Of Integer)
Display(Of Integer)(list)
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
ButClicked = 1
Dim list As MyGenericList(Of String)
Display(Of String)(list)
End Sub
End Class
Last edited by GrimmReaper; December 30th, 2008 at 04:52 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|