By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Binary Search Tree - CompareTo Error

P: n/a
Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*****************************
The class Person
*****************************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam() And Me.isValidAnaam() Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))
bstLedenLijst.Add(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.Add(New Persoon("Demi", "Moore"))
bstLedenLijst.Add(New Persoon("Julia", "Roberts"))
bstLedenLijst.Add(New Persoon("Andie", "MacDowell"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class

Dec 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf
Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
Dim bstLedenLijst As New BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(Of Persoon)
NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotmail.comwrote in message
news:CN**********************@phobos.telenet-ops.be...
Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*****************************
The class Person
*****************************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam() And Me.isValidAnaam() Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))
bstLedenLijst.Add(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.Add(New Persoon("Demi", "Moore"))
bstLedenLijst.Add(New Persoon("Julia", "Roberts"))
bstLedenLijst.Add(New Persoon("Andie", "MacDowell"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class
Dec 28 '06 #2

P: n/a
Doh!

Clicked send too soon. I had the trees backwards:

Dim bstLedenLijst As New BinarySearchTree(Of Persoon)
Dim bstLidnummer As New BinarySearchTree(Of String)

Of course this demonstrated the immediate & obvious compile time error I
suggested ;-)

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotmail.comwrote in message
news:CN**********************@phobos.telenet-ops.be...
Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*****************************
The class Person
*****************************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam() And Me.isValidAnaam() Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))
bstLedenLijst.Add(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.Add(New Persoon("Demi", "Moore"))
bstLedenLijst.Add(New Persoon("Julia", "Roberts"))
bstLedenLijst.Add(New Persoon("Andie", "MacDowell"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class
Dec 28 '06 #3

P: n/a
Reviewing this, one more change:

Public Function Search(ByVal data As T) As T
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As T

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

Note that Search returns type T; this ensures that BinarySearchTree(Of T) is
type safe and performant. performant in that the data will not be boxed. (in
the case of BinarySearchTree(Of Integer))

Although I would recommend defining Search as:

Public Function Search(ByVal match As Predicate(Of T)) As T

Or even:

Public Delegate Function Predicate(Of T, V)(ByVal obj As T, ByVal value
As V) As Boolean

Public Function Search(Of V)(ByVal value As V, ByVal match As
Predicate(Of T, V)) As T

Which I will leave as an exercise for you to complete...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow" <Ja************@tsbradley.netwrote in message
news:eI**************@TK2MSFTNGP04.phx.gbl...
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
> Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
> Dim bstLedenLijst As New BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(Of Persoon)

NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotmail.comwrote in message
news:CN**********************@phobos.telenet-ops.be...
>Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************************************************* *
Class BST
************************************************* *
Public Class BinarySearchTree
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*****************************
The class Person
*****************************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam() And Me.isValidAnaam() Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))
bstLedenLijst.Add(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.Add(New Persoon("Demi", "Moore"))
bstLedenLijst.Add(New Persoon("Julia", "Roberts"))
bstLedenLijst.Add(New Persoon("Andie", "MacDowell"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class
Dec 28 '06 #4

P: n/a
Thank you for your quick response; I solved afther the poste was made the
promlem this way ..

Dim zoekPersoon As New Persoon(Me.txtZoek.Text, "")

Console.WriteLine(bstLedenLijst.Search(zoekPersoon ))

"Jay B. Harlow" <Ja************@tsbradley.netschreef in bericht
news:eI**************@TK2MSFTNGP04.phx.gbl...
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
> Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
> Dim bstLedenLijst As New BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(Of Persoon)

NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotmail.comwrote in message
news:CN**********************@phobos.telenet-ops.be...
>Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************************************************* *
Class BST
************************************************* *
Public Class BinarySearchTree
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo(Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo(node.Data) '****** this
is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*****************************
The class Person
*****************************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam() And Me.isValidAnaam() Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.Length 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))
bstLedenLijst.Add(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.Add(New Persoon("Demi", "Moore"))
bstLedenLijst.Add(New Persoon("Julia", "Roberts"))
bstLedenLijst.Add(New Persoon("Andie", "MacDowell"))

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class

Dec 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.