Converting c# to vb.net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    #1

    Converting c# to vb.net

    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 ArgumentOutOfRa ngeException()
    End If
    End Function

    Function GetChild(ByVal index As Integer) As ITree(Of T) Implements
    ITree(Of T).GetChild
    Return Me.GetChild(ind ex)
    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.Collecti ons.IEnumerable

    Function GetEnumerator() As System.Collecti ons.IEnumerator Implements
    System.Collecti ons.IEnumerable .GetEnumerator
    Return Me.GetEnumerato r()
    End Function

    Public Function Contains(ByVal item As T) As Boolean
    Using enumerator As IEnumerator(Of T) = Me.GetEnumerato r()
    ...
    Error 8 Option Strict On disallows implicit conversions from
    'System.Collect ions.IEnumerato r' to
    'System.Collect ions.Generic.IE numerator(Of T)'.


  • =?Utf-8?B?RGF2aWQgQW50b24=?=

    #2
    RE: Converting c# to vb.net

    Post the original C# code. Most of the on-line converter don't handle
    generics accurately, so the conversion is incorrect.
    --
    David Anton
    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

    Convert between VB, C#, C++, and Python
    Instant C#
    Instant VB
    Instant C++
    Instant Python
    C++ to C# Converter
    C++ to VB Converter


    "Simon Woods" wrote:
    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 ArgumentOutOfRa ngeException()
    End If
    End Function
    >
    Function GetChild(ByVal index As Integer) As ITree(Of T) Implements
    ITree(Of T).GetChild
    Return Me.GetChild(ind ex)
    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.Collecti ons.IEnumerable
    >
    Function GetEnumerator() As System.Collecti ons.IEnumerator Implements
    System.Collecti ons.IEnumerable .GetEnumerator
    Return Me.GetEnumerato r()
    End Function
    >
    Public Function Contains(ByVal item As T) As Boolean
    Using enumerator As IEnumerator(Of T) = Me.GetEnumerato r()
    ...
    Error 8 Option Strict On disallows implicit conversions from
    'System.Collect ions.IEnumerato r' to
    'System.Collect ions.Generic.IE numerator(Of T)'.
    >
    >
    >

    Comment

    Working...