B
BenCoo
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
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