VB arraylist在另一个类中访问

时间:2013-10-15 01:23:15

标签: vb.net arraylist

我正在为一门课程编写POS应用程序。我在另一个类中访问ArrayList类型的itemList时遇到问题。下面是我的两个类的代码。是否可以访问另一个类中的arrayList(即在Transaction内)?

Public Class Item

    'Declares item variables
    Public itemName As String
    Public itemPrice As Decimal
    Public itemQty As Integer
    Public itemSku As Long
    Public itemList As New ArrayList
    Public newItem As Item


    'Method passes details of item
    Public Sub AddItem(itemSku, itemName, itemPrice, itemQty)
        itemSku = newItem.itemSku
        itemName = newItem.itemName
        itemPrice = newItem.itemPrice
        itemQty = newItem.itemQty
        itemList.Add(newItem) 'adds newItem to arrayList of items itemList
    End Sub


End Class


Public Class Transaction
    Dim subtotal As Decimal
    Dim tax As Decimal
    Dim total As Decimal
    Dim paymentType As String

    Public Function calculateBalance()
        For count As Integer = 0 To itemList.Count 'TRYING TO ACCESS ARRAYLIST HERE

        Next
    End Function
End Class

1 个答案:

答案 0 :(得分:2)

问题是您没有指出应该使用Item的类itemList的实际实例。将功能更改为:

Public Function calculateBalance(myItem As Item)
    For count As Integer = 0 To myItem.itemList.Count 'TRYING TO ACCESS ARRAYLIST HERE

    Next
End Function