0

I'm writing a POS application for a course. I'm having trouble accessing itemList of type ArrayList in a different class. Below is my code of the two classes. Is it possible to access an arrayList in another class (i.e. inside 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 Answer 1

2

The problem is that you're not indicating an actual instance of class Item whose itemList should be used. Change the function to:

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

    Next
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

In order for this to work do I need to generate a field of type item?
Yep, you need to instantiate an Item object somewhere else, then pass it to the function. As an alternative, you could pass just the itemList to the function if the rest of the Item isn't used by the function. It all depends on what exactly you're trying to do with your program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.