Option Explicit On
Public Class Person
Private m_strFirstName As String
Private m_strLastName As String
Private m_strDepartment As String
Private m_strEmail As String
Private m_strPhone As String
Public Property FirstName() As String
Get
Return m_strFirstName
End Get
Set(ByVal Value As String)
m_strFirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return m_strLastName
End Get
Set(ByVal Value As String)
m_strLastName = Value
End Set
End Property
Public Property Department() As String
Get
Return m_strDepartment
End Get
Set(ByVal Value As String)
m_strDepartment = Value
End Set
End Property
Public Property Email() As String
Get
Return m_strEmail
End Get
Set(ByVal Value As String)
m_strEmail = Value
End Set
End Property
Public Property Phone() As String
Get
Return m_strPhone
End Get
Set(ByVal Value As String)
m_strPhone = Value
End Set
End Property
Public Sub New(ByVal strFirstName As String, ByVal strLastName As String, ByVal strDepartment As String, ByVal strEmail As String, ByVal strPhone As String)
FirstName = strFirstName
LastName = strLastName
Department = strDepartment
Email = strEmail
Phone = strPhone
End Sub
End Class
Public Class PersonCollection
Inherits System.Collections.CollectionBase
' Restricts to Person types, items that can be added to the collection.
Public Sub Add(ByVal aPerson As Person)
' Invokes Add method of the List object to add a Person.
List.Add(aPerson)
End Sub
Public Sub Remove(ByVal index As Integer)
' Check to see if there is a Person at the supplied index.
If index > Count - 1 Or index < 0 Then
' If no Person 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 Person object.
Public ReadOnly Property Item(ByVal index As Integer) As Person
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the Person type, then returned to the
' caller.
Return CType(List.Item(index), Person)
End Get
End Property
Public Sub Load(ByVal strTextFile As String)
'Loads the information in the comma delimited text file
'Format: "firstname, lastname, department, email, phone"
Dim sr As New IO.StreamReader(strTextFile)
Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
sr.Close()
sr = Nothing
For I As Integer = 0 To strFile.Length - 1
Dim strTmp() As String = Split(strFile(I), " ,")
Me.Add(New Person(strTmp(0), strTmp(1), strTmp(2), strTmp(3), strTmp(4)))
Next
'Job Done
End Sub
End Class