473,748 Members | 5,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Linked List

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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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

Sep 12 '08 #1
11 2550
Ok, I'm continuing to try this on my own. Here's what I have. It won't
compile yet so I'm not sure if I'm on the right track, but I thought I'd
keep giving it a shot. There are no error indications in my Form1_Load or my
LinkedListItem class.

My problem is in the LinkedList class (which hasn't been changed since the
last post).

Private _Head As LinkedListItem <--- This has to have a type declaration
(i.e. LinkedListItem( Of String)). At this point, the program doesn't know
what type will be passed. Where am I going wrong?

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add(New LinkedListItem( Of String)("12345" ))
MyList.Add(New LinkedListItem( Of Integer)(55))
MyList.Add(New LinkedListItem( Of String)("Some words"))
MyList.Add(New LinkedListItem( Of Double)(1.234))

Dim i = MyList.GetFirst Item

Do While Not i Is Nothing
TextBox1.Text += i.Value & vbCrLf
i = i.NextItem
Loop
End Sub
End Class

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( Of T)
Private _Data As T
Private _NextLink As LinkedListItem( Of T)

Public Property Value() As T
Get
Return _Data
End Get
Set(ByVal value As T)
_Data = value
End Set
End Property

Public Property NextItem() As LinkedListItem( Of T)
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem( Of T))
_NextLink = value
End Set
End Property

Public Sub New(ByVal Data As T)
_Data = Data
End Sub
End Class
"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:u8******** ******@TK2MSFTN GP05.phx.gbl...
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 12 '08 #2
Scott,

This is asked a thousand times to show that you can only use scripting and
that early binding is a fake.

However what kind of application are you building that you don't know which
values types are used.

Strongly typed means that the value type is known before runtime starts.

However, as you want this, then use reflection, thousand of persons have
been before you going this way, with the same result: The were using
strongly typed at the time they understood it.

Cor

"Scott Stark" <sc***@webservi cesinc.comschre ef in bericht
news:u8******** ******@TK2MSFTN GP05.phx.gbl...
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 13 '08 #3
Actually, it's an exercise in Chapter 1 of the prep book for the MCTS 70-536
exam.

"Create a linked-list generic class that enables you to create a chain of
different object types."

I've been trying to accomplish this on my own for about three days and have
finally decided to ask for help moving in the right direction. I've gotten
all of the other "Suggested Practices" down, just not this one. Given that
it's in Chapter 1 and they haven't even touched on reflection yet, I'm
assuming there's a simpler way to do it.

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:54******** *************** ***********@mic rosoft.com...
Scott,

This is asked a thousand times to show that you can only use scripting and
that early binding is a fake.

However what kind of application are you building that you don't know
which values types are used.

Strongly typed means that the value type is known before runtime starts.

However, as you want this, then use reflection, thousand of persons have
been before you going this way, with the same result: The were using
strongly typed at the time they understood it.

Cor

"Scott Stark" <sc***@webservi cesinc.comschre ef in bericht
news:u8******** ******@TK2MSFTN GP05.phx.gbl...
>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(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 13 '08 #4
Scott,

I don't have that book at hand now, I will see as nobody has answered if I
find your problem at monday.

Cor

"Scott Stark" <sc***@webservi cesinc.comschre ef in bericht
news:Ok******** ******@TK2MSFTN GP03.phx.gbl...
Actually, it's an exercise in Chapter 1 of the prep book for the MCTS
70-536 exam.

"Create a linked-list generic class that enables you to create a chain of
different object types."

I've been trying to accomplish this on my own for about three days and
have finally decided to ask for help moving in the right direction. I've
gotten all of the other "Suggested Practices" down, just not this one.
Given that it's in Chapter 1 and they haven't even touched on reflection
yet, I'm assuming there's a simpler way to do it.

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:54******** *************** ***********@mic rosoft.com...
>Scott,

This is asked a thousand times to show that you can only use scripting
and that early binding is a fake.

However what kind of application are you building that you don't know
which values types are used.

Strongly typed means that the value type is known before runtime starts.

However, as you want this, then use reflection, thousand of persons have
been before you going this way, with the same result: The were using
strongly typed at the time they understood it.

Cor

"Scott Stark" <sc***@webservi cesinc.comschre ef in bericht
news:u8******* *******@TK2MSFT NGP05.phx.gbl.. .
>>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
LinkedListIte m(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(ByVa l sender As System.Object, ByVal e As
System.EventA rgs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 13 '08 #5
Hi Scott,
My problem is in the LinkedList class (which hasn't been changed since the
last post).

Private _Head As LinkedListItem <--- This has to have a type declaration
(i.e. LinkedListItem( Of String)). At this point, the program doesn't know
what type will be passed. Where am I going wrong?
There's two typical design patterns used for this:

The first, and most common is for the LinkedList to be generic,
LinkedList(Of T) and each item to be a LinkedListItem( Of T). In your case
however you are using different types, so the generic would need to be
instantiated as LinkedList(Of Object) and LinkedListItem( Of Object), which
is seldom of any value ;)

The other kind of pattern you can use is an interface or an abstract base
class, eg:

Public MustIntherit CLass LinkedListItem
Private _NextLink As LinkedListItem

Public Property NextItem() As LinkedListItem
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem( Of T))
_NextLink = value
End Set
End Property

End CLass
Public Class LinkedListItem( Of T)
Inherits LinkedListItem

Private _Data As T

Public Property Value() As T
Get
Return _Data
End Get
Set(ByVal value As T)
_Data = value
End Set
End Property

Public Sub New(ByVal Data As T)
_Data = Data
End Sub
End Class
Although that may work, again you will have an issue actually getting the
data from the linked list as you would need to know the generic type before
hand, which makes it kind of useless in practice. You could have a
MustOverride property Data As Object, but then you have gone full circle,
and back to object. And reality is, that is the crux of your problem. you
have different types and their only common base is Object, so generics isn't
really a solution in those cases.

BTW: I strongly suggest you turn Option Strict On.

"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:up******** ******@TK2MSFTN GP05.phx.gbl...
Ok, I'm continuing to try this on my own. Here's what I have. It won't
compile yet so I'm not sure if I'm on the right track, but I thought I'd
keep giving it a shot. There are no error indications in my Form1_Load or
my LinkedListItem class.

My problem is in the LinkedList class (which hasn't been changed since the
last post).

Private _Head As LinkedListItem <--- This has to have a type declaration
(i.e. LinkedListItem( Of String)). At this point, the program doesn't know
what type will be passed. Where am I going wrong?

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add(New LinkedListItem( Of String)("12345" ))
MyList.Add(New LinkedListItem( Of Integer)(55))
MyList.Add(New LinkedListItem( Of String)("Some words"))
MyList.Add(New LinkedListItem( Of Double)(1.234))

Dim i = MyList.GetFirst Item

Do While Not i Is Nothing
TextBox1.Text += i.Value & vbCrLf
i = i.NextItem
Loop
End Sub
End Class

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( Of T)
Private _Data As T
Private _NextLink As LinkedListItem( Of T)

Public Property Value() As T
Get
Return _Data
End Get
Set(ByVal value As T)
_Data = value
End Set
End Property

Public Property NextItem() As LinkedListItem( Of T)
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem( Of T))
_NextLink = value
End Set
End Property

Public Sub New(ByVal Data As T)
_Data = Data
End Sub
End Class
"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:u8******** ******@TK2MSFTN GP05.phx.gbl...
>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(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 13 '08 #6
Hi Bill,

That was basically the problem I was running into. I can just cast the type
to Object, but then I might as well just not use generics, as you said.

I told the other poster that I'm doing this as an exercise in Chapter 1 of
the prep book for the MCTS 70-536 exam. Specifically, the question states:

"Create a linked-list generic class that enables you to create a chain of
different object types."

I'm at a total loss as to how to accomplish this without incuring boxing and
unboxing penalties by using Object, which seems to defeat the purpose of
this exercise in generics...

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:EB******** *************** ***********@mic rosoft.com...
Hi Scott,
>My problem is in the LinkedList class (which hasn't been changed since
the last post).

Private _Head As LinkedListItem <--- This has to have a type
declaration (i.e. LinkedListItem( Of String)). At this point, the program
doesn't know what type will be passed. Where am I going wrong?

There's two typical design patterns used for this:

The first, and most common is for the LinkedList to be generic,
LinkedList(Of T) and each item to be a LinkedListItem( Of T). In your case
however you are using different types, so the generic would need to be
instantiated as LinkedList(Of Object) and LinkedListItem( Of Object), which
is seldom of any value ;)

The other kind of pattern you can use is an interface or an abstract base
class, eg:

Public MustIntherit CLass LinkedListItem
Private _NextLink As LinkedListItem

Public Property NextItem() As LinkedListItem
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem( Of T))
_NextLink = value
End Set
End Property

End CLass
Public Class LinkedListItem( Of T)
Inherits LinkedListItem

Private _Data As T

Public Property Value() As T
Get
Return _Data
End Get
Set(ByVal value As T)
_Data = value
End Set
End Property

Public Sub New(ByVal Data As T)
_Data = Data
End Sub
End Class
Although that may work, again you will have an issue actually getting the
data from the linked list as you would need to know the generic type
before hand, which makes it kind of useless in practice. You could have a
MustOverride property Data As Object, but then you have gone full circle,
and back to object. And reality is, that is the crux of your problem.
you have different types and their only common base is Object, so generics
isn't really a solution in those cases.

BTW: I strongly suggest you turn Option Strict On.

"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:up******** ******@TK2MSFTN GP05.phx.gbl...
>Ok, I'm continuing to try this on my own. Here's what I have. It won't
compile yet so I'm not sure if I'm on the right track, but I thought I'd
keep giving it a shot. There are no error indications in my Form1_Load or
my LinkedListItem class.

My problem is in the LinkedList class (which hasn't been changed since
the last post).

Private _Head As LinkedListItem <--- This has to have a type
declaration (i.e. LinkedListItem( Of String)). At this point, the program
doesn't know what type will be passed. Where am I going wrong?

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add(New LinkedListItem( Of String)("12345" ))
MyList.Add(New LinkedListItem( Of Integer)(55))
MyList.Add(New LinkedListItem( Of String)("Some words"))
MyList.Add(New LinkedListItem( Of Double)(1.234))

Dim i = MyList.GetFirst Item

Do While Not i Is Nothing
TextBox1.Text += i.Value & vbCrLf
i = i.NextItem
Loop
End Sub
End Class

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( Of T)
Private _Data As T
Private _NextLink As LinkedListItem( Of T)

Public Property Value() As T
Get
Return _Data
End Get
Set(ByVal value As T)
_Data = value
End Set
End Property

Public Property NextItem() As LinkedListItem( Of T)
Get
Return _NextLink
End Get
Set(ByVal value As LinkedListItem( Of T))
_NextLink = value
End Set
End Property

Public Sub New(ByVal Data As T)
_Data = Data
End Sub
End Class
"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:u8******* *******@TK2MSFT NGP05.phx.gbl.. .
>>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
LinkedListIte m(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(ByVa l sender As System.Object, ByVal e As
System.EventA rgs) Handles MyBase.Load
Dim MyList As New LinkedList
MyList.Add("123 45")
MyList.Add(55)
MyList.Add("Kat hy")
MyList.Add(1.23 4)
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
Sep 14 '08 #7
Hi Scott,

"Scott Stark" <sc***@webservi cesinc.comwrote in message
news:O%******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi Bill,

That was basically the problem I was running into. I can just cast the
type to Object, but then I might as well just not use generics, as you
said.

I told the other poster that I'm doing this as an exercise in Chapter 1 of
the prep book for the MCTS 70-536 exam. Specifically, the question states:

"Create a linked-list generic class that enables you to create a chain of
different object types."
I'm guessing, but sounds like they worded that poorly. I think they just
want you to create a generic linked list where all the members are of the
same type and access to them is strongly typed. I think what they were
trying to do was explain that the generic type allows you to specify
different types, but each instantiation of the linked list has only one type
for it's items
I'm at a total loss as to how to accomplish this without incuring boxing
and unboxing penalties by using Object, which seems to defeat the purpose
of this exercise in generics...

FWIW: You'll only get boxing with value types. But the real benefit of
generics, especially when you have Option Strict On, is you have strongly
typed access which allows for greater design time feedback and compile time
type checking.

Sep 14 '08 #8
Scott Stark wrote:
Hi Bill,

That was basically the problem I was running into. I can just cast
the type to Object, but then I might as well just not use generics,
as you said.
I told the other poster that I'm doing this as an exercise in Chapter
1 of the prep book for the MCTS 70-536 exam. Specifically, the
question states:
"Create a linked-list generic class that enables you to create a
chain of different object types."

I'm at a total loss as to how to accomplish this without incuring
boxing and unboxing penalties by using Object, which seems to defeat
the purpose of this exercise in generics...
Is it supposed to be an exercise in Generics?

The question could be interpreted two ways:

1. (real Generics) Create a linked-list (Of T) generic class that enables you to
create a chain of objects, all of the same type, where the type can be specified
at the point in your code where the list will be used. Examples of use would be
Dim A As LinkList(Of String)
Dim B As LinkList(Of Integer)

2. (not real Generics, but a general purpose, or "generic", linked list) Create
a linked-list class that enables you to create a chain of objects of mixed type,
by handling all objects as type Object.
Sep 14 '08 #9
It's supposed to be an exercise in generics, and keep in mind it's from the
very first chapter in the book so it's probably supposed to be relatively
simple.
Dim A As LinkList(Of String)
Dim B As LinkList(Of Integer)
This, I've got down. Pretty straightforward , simplistic approach. I also am
starting to believe this is all they are asking for, in which case I wasted
a whole lot of time, LOL.
2. (not real Generics, but a general purpose, or "generic", linked list)
Create a linked-list class that enables you to create a chain of objects
of mixed type, by handling all objects as type Object.
I also have a linked list class written to do just this. My interpretation
of the question was that they wanted a combination of the two apporaches,
something like:

Dim A As LinkList(Of T) (which i know is not syntactically correct)
A.Add(1.234)
A.Add("Some words"
A.Add(New MailMessage())

Etc...

Doesn't seem that this is possible without casting everything as an Object,
which was my initial trouble spot. I think the question is just a bit
misleading.

"Steve Gerrard" <my********@com cast.netwrote in message
news:l_******** *************** *******@comcast .com...
Scott Stark wrote:
>Hi Bill,

That was basically the problem I was running into. I can just cast
the type to Object, but then I might as well just not use generics,
as you said.
I told the other poster that I'm doing this as an exercise in Chapter
1 of the prep book for the MCTS 70-536 exam. Specifically, the
question states:
"Create a linked-list generic class that enables you to create a
chain of different object types."

I'm at a total loss as to how to accomplish this without incuring
boxing and unboxing penalties by using Object, which seems to defeat
the purpose of this exercise in generics...

Is it supposed to be an exercise in Generics?

The question could be interpreted two ways:

1. (real Generics) Create a linked-list (Of T) generic class that enables
you to create a chain of objects, all of the same type, where the type can
be specified at the point in your code where the list will be used.
Examples of use would be
Dim A As LinkList(Of String)
Dim B As LinkList(Of Integer)

2. (not real Generics, but a general purpose, or "generic", linked list)
Create a linked-list class that enables you to create a chain of objects
of mixed type, by handling all objects as type Object.

Sep 14 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4162
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: struct node { struct node *next; void *data; };
6
6963
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class DisJunctiveConstraintList : LinkedList<DisjunctiveConstraint>
10
1937
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that I will need the Generic Class, given there are so many other options. -- cheers,
7
2843
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
3
2499
by: swetha bandaru | last post by:
what is meant by generic linked list.How to implement it in c? can anyone plz help me.
4
5113
by: sengupta_arka123 | last post by:
How do I implement a generic linked list?
0
8630
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
4
3267
by: Tony Johansson | last post by:
Hello! What actually does this sentence mean? Create a linked-list generic class that enables you to create a chain of different objects types. When I create a linked-list generic class for example with the string type. I do the following. List<stringmyList = new List<string>();
7
5772
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.