locked
Convert C# to VB (CartLine) RRS feed

  • Question

  • User-2089506584 posted

    Hi,

    I have this code taken from the book of Sanders "Pro ASP.NET MVC 1.0"  and I don't get the conversion from C# to VB of the following code below. Error is "'Lines' is already declared as 'Private lines As System.Collections.Generic.List(Of CartLine)' in this class."

    C#



    namespace DomainModel.Entities
    {
    public class Cart
    {
    private List<CartLine> lines = new List<CartLine>();
    public IList<CartLine> Lines { get { return lines; } }
    public void AddItem(Product product, int quantity) { }
    public decimal ComputeTotalValue() { throw new NotImplementedException(); }
    public void Clear() { throw new NotImplementedException(); }
    }
    public class CartLine
    {
    public Product Product { get; set; }
    public int Quantity { get; set; }
    }
    }

    As the error implies, I know such variable already exist but isn't what the C# version did? Or I am wrong.

    VB
    Namespace DomainModel.Entities

    Public Class Cart

    Private lines As List(Of CartLine) = New List(Of CartLine)

    Public ReadOnly Property Lines() As IList(Of CartLine)
    Get
    Return Lines
    End Get
    End Property

    Public Sub AddItem(ByVal product As Product, ByVal quantity As Integer)

    End Sub

    Public Function ComputeTotalValue() As Decimal
    Throw New NotImplementedException
    End Function

    Public Sub Clear()
    Throw New NotImplementedException
    End Sub

    End Class

    Public Class CartLine

    Dim _Product As Product
    Public Property Product() As Product
    Get
    Return _Product
    End Get
    Set(ByVal value As Product)
    _Product = value
    End Set
    End Property

    Dim _Quantity As Integer
    Public Property Quantity() As Integer
    Get
    Return Quantity
    End Get
    Set(ByVal value As Integer)
    _Quantity = value
    End Set
    End Property
    End Class

    End Namespace


    Saturday, September 12, 2009 3:28 AM

Answers

  • User397347636 posted

    VB does not allow you to declare a field with the same name as a method in the class.  Since VB is not case-sensitive, "Lines" is regarded the same as "lines" (C# is case-sensitive, so it has no problem with this.)

    The correct VB9 conversion of this code is:

    Namespace DomainModel.Entities
    	Public Class Cart
    'INSTANT VB NOTE: The variable lines was renamed since Visual Basic does not allow class members with the same name:
    		Private lines_Renamed As New List(Of CartLine)()
    		Public ReadOnly Property Lines() As IList(Of CartLine)
    			Get
    				Return lines_Renamed
    			End Get
    		End Property
    		Public Sub AddItem(ByVal product As Product, ByVal quantity As Integer)
    		End Sub
    		Public Function ComputeTotalValue() As Decimal
    			Throw New NotImplementedException()
    		End Function
    		Public Sub Clear()
    			Throw New NotImplementedException()
    		End Sub
    	End Class
    	Public Class CartLine
    		Private privateProduct As Product
    		Public Property Product() As Product
    			Get
    				Return privateProduct
    			End Get
    			Set(ByVal value As Product)
    				privateProduct = value
    			End Set
    		End Property
    		Private privateQuantity As Integer
    		Public Property Quantity() As Integer
    			Get
    				Return privateQuantity
    			End Get
    			Set(ByVal value As Integer)
    				privateQuantity = value
    			End Set
    		End Property
    	End Class
    End Namespace


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 12, 2009 9:51 AM

All replies

  • User1506406490 posted

     Hi good day

     i had gone through u's code.As i check that,r u trying to covert the code of c# to VB.In this case the error is only for C#,as may be in declaring your private & public methods,class the error evolves.Check with the variable name 'lines' etc,hope u code will work.

    good luckCool 

    Saturday, September 12, 2009 8:59 AM
  • User-2089506584 posted

    In this case the error is only for C#

    Are you saying the code in the book has errors? Just like the above code?

    Is there any workaround on this?


    Namespace DomainModel.Entities

        Public Class Cart

            Private lines As List(Of CartLine) = New List(Of CartLine)
            Public ReadOnly Property Lines() As IList(Of CartLine)
                Get
                    Return _Lines
                End Get
            End Property


            Public Sub AddItem(ByVal product As Product, ByVal quantity As Integer)
                Dim line = Lines.FirstOrDefault(Function(l) (l.Product.ProductID = product.ProductID))
                If line Is Nothing Then
                    Lines.Add(New CartLine() With {.Product = product, .Quantity = quantity})
                Else
                    line.Quantity = (line.Quantity + quantity)
                End If
            End Sub

            Public Function ComputeTotalValue() As Decimal
                Throw New NotImplementedException
            End Function

            Public Sub Clear()
                Throw New NotImplementedException
            End Sub

        End Class

        Public Class CartLine

            Dim _Product As Product
            Public Property Product() As Product
                Get
                    Return _Product
                End Get
                Set(ByVal value As Product)
                    _Product = value
                End Set
            End Property

            Dim _Quantity As Integer
            Public Property Quantity() As Integer
                Get
                    Return Quantity
                End Get
                Set(ByVal value As Integer)
                    _Quantity = value
                End Set
            End Property
        End Class

    End Namespace

    Saturday, September 12, 2009 9:48 AM
  • User397347636 posted

    VB does not allow you to declare a field with the same name as a method in the class.  Since VB is not case-sensitive, "Lines" is regarded the same as "lines" (C# is case-sensitive, so it has no problem with this.)

    The correct VB9 conversion of this code is:

    Namespace DomainModel.Entities
    	Public Class Cart
    'INSTANT VB NOTE: The variable lines was renamed since Visual Basic does not allow class members with the same name:
    		Private lines_Renamed As New List(Of CartLine)()
    		Public ReadOnly Property Lines() As IList(Of CartLine)
    			Get
    				Return lines_Renamed
    			End Get
    		End Property
    		Public Sub AddItem(ByVal product As Product, ByVal quantity As Integer)
    		End Sub
    		Public Function ComputeTotalValue() As Decimal
    			Throw New NotImplementedException()
    		End Function
    		Public Sub Clear()
    			Throw New NotImplementedException()
    		End Sub
    	End Class
    	Public Class CartLine
    		Private privateProduct As Product
    		Public Property Product() As Product
    			Get
    				Return privateProduct
    			End Get
    			Set(ByVal value As Product)
    				privateProduct = value
    			End Set
    		End Property
    		Private privateQuantity As Integer
    		Public Property Quantity() As Integer
    			Get
    				Return privateQuantity
    			End Get
    			Set(ByVal value As Integer)
    				privateQuantity = value
    			End Set
    		End Property
    	End Class
    End Namespace


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 12, 2009 9:51 AM
  • User-2089506584 posted

    Since VB is not case-sensitive, "Lines" is regarded the same as "lines" (C# is case-sensitive, so it has no problem with this.)

    Thank YOu so much DAvid! cheers!

    Saturday, September 12, 2009 11:26 PM