S
Simon Woods
Hi
I've run some binary tree c# code on various on-line converters. The code
compiles fine in c# but raises a few issues in vb.net. This is all pretty
new to me, so if this is obvious pls excuse me, but I wonder if someone
could explain why vb complains, and possible work rounds.
Thanks
Simon
a) vb complains of the following
Public Sub New(ByVal data As T)
Me.New(data, Nothing, Nothing)
End Sub
"Error 1 Overload resolution failed because no accessible 'New' is most
specific for these arguments:
'Public Sub New(data As T, left As BinaryTree(Of T), right As
BinaryTree(Of T))': Not most specific.
'Public Sub New(data As T, left As T, right As T)': Not most specific."
Public Sub New(ByVal data As T, ByVal left As T, ByVal right As T)
Me.New(data, New BinaryTree(Of T)(left), New BinaryTree(Of
T)(right))
End Sub
Public Sub New(ByVal data As T, ByVal left As BinaryTree(Of T), ByVal right
As BinaryTree(Of T))
m_leftSubtree = lef
m_rightSubtree = right
Me.m_data = data
End Sub
b)
Public Interface ITree(Of T)
Sub Add(ByVal child As ITree(Of T))
Function GetChild(ByVal index As Integer) As ITree(Of T)
....
End Interface
Public Class BinaryTree(Of T)
Implements ITree(Of T)
Private m_leftSubtree As BinaryTree(Of T)
Private m_rightSubtree As BinaryTree(Of T)
Private m_data As T
.....
Public Function GetChild(ByVal index As Integer) As BinaryTree(Of T)
If index = 0 Then
Return m_leftSubtree
ElseIf index = 1 Then
Return m_rightSubtree
Else
Throw New ArgumentOutOfRangeException()
End If
End Function
Function GetChild(ByVal index As Integer) As ITree(Of T) Implements
ITree(Of T).GetChild
Return Me.GetChild(index)
End Function
Error 7 'Public Function GetChild(index As Integer) As ITree(Of T)' and
'Public Function GetChild(index As Integer) As BinaryTree(Of T)' cannot
overload each other because they differ only by return types.
c)
Public Class BinaryTree(Of T)
Implements System.Collections.IEnumerable
Function GetEnumerator() As System.Collections.IEnumerator Implements
System.Collections.IEnumerable.GetEnumerator
Return Me.GetEnumerator()
End Function
Public Function Contains(ByVal item As T) As Boolean
Using enumerator As IEnumerator(Of T) = Me.GetEnumerator()
...
Error 8 Option Strict On disallows implicit conversions from
'System.Collections.IEnumerator' to
'System.Collections.Generic.IEnumerator(Of T)'.
I've run some binary tree c# code on various on-line converters. The code
compiles fine in c# but raises a few issues in vb.net. This is all pretty
new to me, so if this is obvious pls excuse me, but I wonder if someone
could explain why vb complains, and possible work rounds.
Thanks
Simon
a) vb complains of the following
Public Sub New(ByVal data As T)
Me.New(data, Nothing, Nothing)
End Sub
"Error 1 Overload resolution failed because no accessible 'New' is most
specific for these arguments:
'Public Sub New(data As T, left As BinaryTree(Of T), right As
BinaryTree(Of T))': Not most specific.
'Public Sub New(data As T, left As T, right As T)': Not most specific."
Public Sub New(ByVal data As T, ByVal left As T, ByVal right As T)
Me.New(data, New BinaryTree(Of T)(left), New BinaryTree(Of
T)(right))
End Sub
Public Sub New(ByVal data As T, ByVal left As BinaryTree(Of T), ByVal right
As BinaryTree(Of T))
m_leftSubtree = lef
m_rightSubtree = right
Me.m_data = data
End Sub
b)
Public Interface ITree(Of T)
Sub Add(ByVal child As ITree(Of T))
Function GetChild(ByVal index As Integer) As ITree(Of T)
....
End Interface
Public Class BinaryTree(Of T)
Implements ITree(Of T)
Private m_leftSubtree As BinaryTree(Of T)
Private m_rightSubtree As BinaryTree(Of T)
Private m_data As T
.....
Public Function GetChild(ByVal index As Integer) As BinaryTree(Of T)
If index = 0 Then
Return m_leftSubtree
ElseIf index = 1 Then
Return m_rightSubtree
Else
Throw New ArgumentOutOfRangeException()
End If
End Function
Function GetChild(ByVal index As Integer) As ITree(Of T) Implements
ITree(Of T).GetChild
Return Me.GetChild(index)
End Function
Error 7 'Public Function GetChild(index As Integer) As ITree(Of T)' and
'Public Function GetChild(index As Integer) As BinaryTree(Of T)' cannot
overload each other because they differ only by return types.
c)
Public Class BinaryTree(Of T)
Implements System.Collections.IEnumerable
Function GetEnumerator() As System.Collections.IEnumerator Implements
System.Collections.IEnumerable.GetEnumerator
Return Me.GetEnumerator()
End Function
Public Function Contains(ByVal item As T) As Boolean
Using enumerator As IEnumerator(Of T) = Me.GetEnumerator()
...
Error 8 Option Strict On disallows implicit conversions from
'System.Collections.IEnumerator' to
'System.Collections.Generic.IEnumerator(Of T)'.