S
Scott Stark
Hello,
The code below represents a singly-linked list that accepts any type of
object.
You can see I'm represting the Data variable a System.Object. How would I
update this code to use generics instead of System.Object. I want the code
in Form1_Load to remain exactly the same, but in the background I want to
use generics. I'm trying to get a better understanding of how it works and
I'm a little stuck.
I tried converting the LinkedListItem class to: Public Class
LinkedListItem(Of T) and then, of course switching the System.Object
variables to type T. But then in the LinkedList class, my _Head and _Tail
objects would need to be case as LinkedListItem(Of T), but at that point it
needs to know the object type (String, Integer, Double, etc). I won't know
the object type until I've added it to the list using my Add() method. I'm
sure it's a simple solution, I'm just stuck on this one.
Thanks in advance.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("12345")
MyList.Add(55)
MyList.Add("Kathy")
MyList.Add(1.234)
End Sub
Public Class LinkedList
Private _Head As LinkedListItem
Private _Tail As LinkedListItem
Public ReadOnly Property GetFirstItem()
Get
Return _Head
End Get
End Property
Public Property Head() As LinkedListItem
Get
Return _Head
End Get
Set(ByVal value As LinkedListItem)
_Head = value
End Set
End Property
Public Sub Add(ByVal Data As Object)
Dim Item As New LinkedListItem(Data)
If (_Head Is Nothing) Then
_Head = Item
End If
If (_Tail Is Nothing) Then
_Tail = Item
Else
_Tail.NextItem = Item
_Tail = Item
End If
End Sub
End Class
Public Class LinkedListItem
Private _Data As Object
Private _NextLink As LinkedListItem
Public Property Value() As Object
Get
Return _Data
End Get
Set(ByVal value As Object)
_Data = value
End Set
End Property
Public Property NextItem() As LinkedListItem
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem)
_NextLink = value
End Set
End Property
Public Sub New(ByVal Data As Object)
_Data = Data
End Sub
End Class
The code below represents a singly-linked list that accepts any type of
object.
You can see I'm represting the Data variable a System.Object. How would I
update this code to use generics instead of System.Object. I want the code
in Form1_Load to remain exactly the same, but in the background I want to
use generics. I'm trying to get a better understanding of how it works and
I'm a little stuck.
I tried converting the LinkedListItem class to: Public Class
LinkedListItem(Of T) and then, of course switching the System.Object
variables to type T. But then in the LinkedList class, my _Head and _Tail
objects would need to be case as LinkedListItem(Of T), but at that point it
needs to know the object type (String, Integer, Double, etc). I won't know
the object type until I've added it to the list using my Add() method. I'm
sure it's a simple solution, I'm just stuck on this one.
Thanks in advance.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("12345")
MyList.Add(55)
MyList.Add("Kathy")
MyList.Add(1.234)
End Sub
Public Class LinkedList
Private _Head As LinkedListItem
Private _Tail As LinkedListItem
Public ReadOnly Property GetFirstItem()
Get
Return _Head
End Get
End Property
Public Property Head() As LinkedListItem
Get
Return _Head
End Get
Set(ByVal value As LinkedListItem)
_Head = value
End Set
End Property
Public Sub Add(ByVal Data As Object)
Dim Item As New LinkedListItem(Data)
If (_Head Is Nothing) Then
_Head = Item
End If
If (_Tail Is Nothing) Then
_Tail = Item
Else
_Tail.NextItem = Item
_Tail = Item
End If
End Sub
End Class
Public Class LinkedListItem
Private _Data As Object
Private _NextLink As LinkedListItem
Public Property Value() As Object
Get
Return _Data
End Get
Set(ByVal value As Object)
_Data = value
End Set
End Property
Public Property NextItem() As LinkedListItem
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem)
_NextLink = value
End Set
End Property
Public Sub New(ByVal Data As Object)
_Data = Data
End Sub
End Class