First of all, since pupilList.List is an array it is zero based so
VB Code:
MsgBox(ListData.list(ListData.count).age)
would refer to a item that is not in the array
Second, The way you set it up you would have to do the following before adding anything
VB Code:
'to get it to work the way you you have it setup now..
ReDim ListData.list(0)
ListData.count = 1
ListData.list(0) = New pupil
MsgBox(ListData.list(ListData.count - 1).age)
The way I would do it...
VB Code:
Option Explicit On
Public Class cPupilCollection
Inherits System.Collections.CollectionBase
' Restricts to cPupil types, items that can be added to the collection.
Public Sub Add(ByVal acPupil As cPupil)
' Invokes Add method of the List object to add a cPupil.
List.Add(acPupil)
End Sub
Public Sub Remove(ByVal index As Integer)
' Check to see if there is a cPupil at the supplied index.
If index > Count - 1 Or index < 0 Then
' If no cPupil exists, a messagebox is shown and the operation is
' cancelled.
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
' Invokes the RemoveAt method of the List object.
List.RemoveAt(index)
End If
End Sub
' This line declares the Item property as ReadOnly, and
' declares that it will return a cPupil object.
Public ReadOnly Property Item(ByVal index As Integer) As cPupil
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the cPupil type, then returned to the
' caller.
Return CType(List.Item(index), cPupil)
End Get
End Property
End Class
VB Code:
Option Explicit On
Public Class cPupil
Private m_strName As String
Private m_sAge As Short ' Short = Int16
Private m_sID As Short
Private m_strPupilClass As String
Public Property PupilName() As String
Get
Return m_strName
End Get
Set(ByVal Value As String)
m_strName = Value
End Set
End Property
Public Property Age() As Short
Get
Return m_sAge
End Get
Set(ByVal Value As Short)
m_sAge = Value
End Set
End Property
Public Property ID() As Short
Get
Return m_sID
End Get
Set(ByVal Value As Short)
m_sID = Value
End Set
End Property
Public Property PupilClass() As String
Get
Return m_strPupilClass
End Get
Set(ByVal Value As String)
m_strPupilClass = Value
End Set
End Property
End Class
VB Code:
'usage:
Dim Test as New PupilCollection
Dim NewPupil as New Pupil
NewPupil.PupilName = "Bob"
NewPupil.Age = 10
Test.Add(NewPupil)