Use ArrayList
, like this:
Dim BoxList As New ArrayList
Dim box = New Box()
box.x = 1
box.y = 2
BoxList.Add(box)
Note: It is recommended that you add a constructor to the Box
class that will accept the x
and y
values, like this:
Public Class Box
Public x As Integer
Public y As Integer
Public Sub New(ByVal _x As Integer, ByVal _y As Integer)
x = _x
y = _y
End Sub
End Class
Now you can shorten your ArrayList
code to this:
Dim BoxList As New ArrayList
BoxList.Add(New Box(1, 2))
To use the values in the ArrayList
you will need to un-box (pun not intended) the value out of the ArrayList
, like this:
For Each box In BoxList
' Use x value, like this
CType(box, Box).x
Next
OR (as Meta-Knight suggested)
For Each box As Box In BoxList
' Now box is typed as Box and not object, so just use it
box.x
Next