0

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

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
4

1 回答 1

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
于 2013-10-15T01:37:31.340 回答