You need to use a collection class, lookup the innerlist and collection Base properties.
Heres a couple of classes I did this with Carraiages and the ability to add multiple characters to the Collection class
using
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
objCarriageCollection.Add(New Carriage(1, 2.4, "Pullman"))
objCarriageCollection.Add(New Carriage(2, 2.3, "Pullman"))
objCarriageCollection.Add(New Carriage(2, 2.3, "Topless"))
objCarriageCollection.Add(New Carriage(4, 3.0, "6Wheels"))
VB Code:
Public Class Carriage
Private FCarriageNo As Integer
Private FCarriageWeight As Decimal
Private FCarriageModel As String
' Add the Default Constructor
Public Sub New()
' Default Constructor without parameter
End Sub
Public Sub New(ByVal CarriageID As Integer, ByVal CarriageWeight As Decimal, ByVal CarriageModel As String)
FCarriageNo = CarriageID
FCarriageWeight = CarriageWeight
FCarriageModel = CarriageModel
End Sub
Public Property CarriageID() As Integer
Get
Return FCarriageNo
End Get
Set(ByVal Value As Integer)
FCarriageNo = Value
End Set
End Property
Public Property pCarriageWeight() As Decimal
Get
Return FCarriageWeight
End Get
Set(ByVal Value As Decimal)
FCarriageWeight = Value
End Set
End Property
Public Property pCarriageModel() As String
Get
Return FCarriageModel
End Get
Set(ByVal Value As String)
FCarriageModel = Value
End Set
End Property
End Class
VB Code:
Public Class CarriageCollection
Inherits CollectionBase
Default Public Overridable Property Item(ByVal index As Integer) As Carriage
Get
'Here you should really catch any exceptions, like ArgumentOutOfRangeExcetion,
'and then rethrow as an InnerException to your own custom exception.
Return DirectCast(Me.InnerList.Item(index), Carriage)
End Get
Set(ByVal Value As Carriage)
Me.InnerList.Item(index) = Value
End Set
End Property
Public Overridable Function Add(ByVal value As Carriage) As Integer
Return Me.InnerList.Add(value)
End Function
Public Overridable Sub AddRange(ByVal c As ICollection)
For Each i As Object In c
If Not TypeOf i Is Carriage Then
'Here you should really throw your own custom exception.
Throw New ArgumentException("All items added to a CarriageCollection must be of type Carriage.", "c")
End If
Me.InnerList.AddRange(c)
Next
End Sub
Public Overridable Function Contains(ByVal item As Carriage) As Boolean
Return Me.InnerList.Contains(item)
End Function
End Class