Encapsulation problem

sgi

New member
Joined
Sep 7, 2006
Messages
1
Programming Experience
3-5
Hi all,

I have a problem encapsulating a class within a class. It all seems to look okay but I get stuck in a loop when calling it eg.

Dim myCart As New Data.Cart
myCart.Items.Add("00000000-0000-0000-0000-000000000000", 1)

All I get is myCart.Items.Items.Items.Items.....

I'm obviously doing something wrong, can anyone help??!

TIA

VB.NET:
Namespace sgi.data
    Public Class Cart
        Public Class item
            Private _productid As String = ""
            Private _quantity As Integer = 0
            Public Property productid() As String
                Get
                    Return _productid
                End Get
                Set(ByVal value As String)
                    _productid = value
                End Set
            End Property
            Public Property quantity() As Integer
                Get
                    Return _quantity
                End Get
                Set(ByVal value As Integer)
                    _quantity = value
                End Set
            End Property
            Public Sub New()
            End Sub
            Public Sub New(ByVal productid As String, ByVal quantity As Integer)
                Me.productid = productid
                Me.quantity = quantity
            End Sub
        End Class
        Protected _item As List(Of item)
        Public Sub New()
            _item = New List(Of item)
        End Sub
        Public Class items
            Inherits Cart
            Public Sub Add(ByVal productid As String, ByVal quantity As Integer)
                _item.Add(New item(productid, quantity))
            End Sub
        End Class
    End Class
End Namespace
 
You've defined your Items class completely incorrectly. It shouldn't inherit the Cart class. It should inherit CollectionBase, thus provide the functionality to behave as a collection of Item objects.

Also, type names should ALWAYS start with an upper case letter. This is not law but it's a convention that there is no justifiable reason not to adhere to.
 
Back
Top