To write to file (in pipe-delimted format), add the following method to class PersonCollection
Public Sub WriteToFile(ByVal path As String)
Dim builder As New System.Text.StringBuilder(
builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "-------------------------
For Each currentPerson As Person In Me
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(vbCrLf)
Next
Dim writer As New System.IO.StreamWriter(pat
writer.Write(builder.ToStr
writer.Close()
End Sub
To write XML instead of pipe-delimted, check out the documentation for the XmlWriter or XmlDataDocument classes
Main Topics
Browse All Topics





by: bisonfur37Posted on 2006-04-13 at 14:41:50ID: 16449717
Public Class PersonComparer
er.Compare comparison", "x")
To comparison", "obj")
h therPerson .DateOfBir th)
oyment eTo(otherP erson.Date OfEmployme nt)
erPerson.F irstName)
son.Name)
String.Emp ty)
---------- ---------- -------" & vbCrLf)
on.Name) on.FirstNa me) on.DateOfB irth.ToStr ing("yyyy- MM-dd")) on.DateOfE mployment. ToString(" yyyy-MM-dd "))
, title)
tion.Count
n("index", index, "The index must be greater than or equal to zero and less than the number of items in the PersonCollection")
Person)
ator _ able.GetEn umerator (Me)
tion.CopyT o
tion.IsSyn chronized
tion.SyncR oot
ator.Curre nt
ator.MoveN ext
ator.Reset
ableField. Name) ableField. DateOfBirt h)
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.ICompar
If Not (TypeOf x Is Person) Then Throw New ArgumentException("Invalid
Return DirectCast(x, Person).CompareTo(y)
End Function
End Class
Public Class Person
Implements IComparable
Public Enum ComparableField
Name
FirstName
DateOfBirth
DateOfEmployment
End Enum
Private _name As String
Private _firstName As String
Private _dob As DateTime
Private _doe As DateTime
Private Shared _comparisonField As ComparableField
Friend Shared Property ComparisonField() As ComparableField
Get
Return _comparisonField
End Get
Set(ByVal Value As ComparableField)
_comparisonField = Value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal Value As String)
_firstName = Value
End Set
End Property
Public Property DateOfBirth() As DateTime
Get
Return _dob
End Get
Set(ByVal Value As DateTime)
_dob = Value
End Set
End Property
Public Property DateOfEmployment() As DateTime
Get
Return _doe
End Get
Set(ByVal Value As DateTime)
_doe = Value
End Set
End Property
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.Compare
If Not TypeOf obj Is Person Then Throw New ArgumentException("Invalid
Dim otherPerson As Person = obj
'ToDo: create CompareTo delegate function and create a PersonCompareToFactory class
Select Case _comparisonField
Case ComparableField.DateOfBirt
Return Me.DateOfBirth.CompareTo(o
Case ComparableField.DateOfEmpl
Return Me.DateOfEmployment.Compar
Case ComparableField.FirstName
Return Me.FirstName.CompareTo(oth
Case ComparableField.Name
Return Me.Name.CompareTo(otherPer
End Select
End Function
Public Sub New(ByVal name As String, ByVal firstName As String, ByVal dob As DateTime, ByVal doe As DateTime)
Me.Name = name
Me.FirstName = firstName
Me.DateOfBirth = dob
Me.DateOfEmployment = doe
End Sub
Public Sub New()
End Sub
End Class
Public Class PersonCollection
Implements IEnumerable
Implements ICollection
Private _people As New ArrayList
Public Sub Display(ByVal title As String)
Dim builder As New System.Text.StringBuilder(
builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "-------------------------
For Each currentPerson As Person In Me
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(" | ")
builder.Append(currentPers
builder.Append(vbCrLf)
Next
MsgBox(builder.ToString(),
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.ICollec
Get
Return _people.Count
End Get
End Property
Default Public ReadOnly Property Item(ByVal index As Integer) As Person
Get
If index < 0 Or index >= _people.Count Then
Throw New ArgumentOutOfRangeExceptio
End If
Return DirectCast(_people(index),
End Get
End Property
Public Sub Add(ByVal thePerson As Person)
_people.Add(thePerson)
End Sub
Public Function GetEnumerator() As System.Collections.IEnumer
Implements System.Collections.IEnumer
Return New PersonCollectionEnumerator
End Function
Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollec
Call _people.CopyTo(array, index)
End Sub
Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollec
Get
Return _people.IsSynchronized
End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollec
Get
Return _people.SyncRoot
End Get
End Property
Public Sub Sort(ByVal sortBy As Person.ComparableField)
Person.ComparisonField = sortBy
_people.Sort(New PersonComparer)
End Sub
Private Class PersonCollectionEnumerator
Implements IEnumerator
Private _index As Integer
Private _personCol As PersonCollection
Public Sub New(ByVal Persons As PersonCollection)
_personCol = Persons
Me.Reset()
End Sub
Public ReadOnly Property Current() As Object Implements System.Collections.IEnumer
Get
Return _personCol.Item(_index)
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumer
_index += 1
Return (_index < _personCol.Count)
End Function
Public Sub Reset() Implements System.Collections.IEnumer
_index = -1
End Sub
End Class
End Class
Public Class Tester
Private _people As New PersonCollection
Private Sub TestPeople()
_people.Add(New Person("John Doe", "John", DateSerial(1930, 1, 1), DateSerial(2006, 3, 12)))
_people.Add(New Person("Homer Simpson", "Homer", DateSerial(1955, 3, 1), DateSerial(1987, 2, 12)))
_people.Add(New Person("Jane Does", "Jane", DateSerial(1973, 8, 8), DateSerial(2006, 3, 20)))
_people.Sort(Person.Compar
_people.Display("Sorted By Name")
_people.Sort(Person.Compar
_people.Display("Sorted By DOB")
End Sub
Shared Sub Main()
Call New Tester().TestPeople()
End Sub
End Class