CStudents类模块
Option Explicit
Private mcolStudents As New Collection
Dim intNum As Integer
Dim intPosition As Long
Dim iStudent As CStudent
Public Function Add(ByVal Name As String) As CStudent
Dim Student As New CStudent
With Student
'为学生产生一个唯一的 ID。
intNum = intNum + 1
.ID = intNum
.Name = Name
'将 CStudent 对象引用添加到集合中,
'用 ID 属性作为键。
mcolStudents.Add Student, "K" & .ID
End With
'为新的 CStudent 返回一个引用。
Set Add = Student
Set iStudent = Student
End Function
Public Function Count() As Long
Count = mcolStudents.Count
End Function
'删除其中的一个学生记录
Public Sub Delete(ByVal Index As Long)
mcolStudents.Remove "K" & Index
End Sub
'CStudents 集合类的方法。
Public Function Item(ByVal Index As Variant) As CStudent
'On Error Resume Next
Set Item = mcolStudents.Item("K" & Index)
End Function
'清除所有学生记录
Public Sub Clear()
Set mcolStudents = Nothing
Set mcolStudents = New Collection
intNum = 0
End Sub
Public Property Get Position() As Long
Position = intPosition
End Property
Public Property Let Position(ByVal vNewValue As Long)
If vNewValue < 1 Or vNewValue > mcolStudents.Count Then
Exit Property
End If
intPosition = vNewValue
Set iStudent = mcolStudents.Item("K" & intPosition)
End Property
Public Sub Move(Index As Long)
If Index < 1 Or Index > mcolStudents.Count Then
Exit Sub
End If
Set iStudent = mcolStudents.Item("K" & Index)
intPosition = Index
End Sub
Public Property Get SelectStudent() As CStudent
Set SelectStudent = iStudent
End Property