Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Sort people in an array

Hi,

Sorry for this 'newbie' question but i'm struggeling with vb.net after a good life in PHP programming..

Question:

User must be able to put in a list of people..
I've got 4 fields, Name, First name, Date of Birth, Dath of employment
So i've got 4 fields on a form, and a button called ADD
What is the best way to 'store' those people? In an array? Because after the 'user' has entered the people, i want to be able to sort people on Employment date or Date of birth and split them out in several 'groups'.
Also, the user must be able to store and retrieve the 'list' in a file (xml?)
Can i do the above without database?

Please provide me with some samplecode..

Regards,

Frank
0
keessef
Asked:
keessef
  • 7
  • 6
1 Solution
 
bisonfur37Commented:
Public Class PersonComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        If Not (TypeOf x Is Person) Then Throw New ArgumentException("Invalid comparison", "x")
        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.CompareTo
        If Not TypeOf obj Is Person Then Throw New ArgumentException("Invalid comparison", "obj")
        Dim otherPerson As Person = obj

        'ToDo: create CompareTo delegate function and create a PersonCompareToFactory class
        Select Case _comparisonField

            Case ComparableField.DateOfBirth
                Return Me.DateOfBirth.CompareTo(otherPerson.DateOfBirth)

            Case ComparableField.DateOfEmployment
                Return Me.DateOfEmployment.CompareTo(otherPerson.DateOfEmployment)

            Case ComparableField.FirstName
                Return Me.FirstName.CompareTo(otherPerson.FirstName)

            Case ComparableField.Name
                Return Me.Name.CompareTo(otherPerson.Name)

        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(String.Empty)

        builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "----------------------------------------------------" & vbCrLf)

        For Each currentPerson As Person In Me
            builder.Append(currentPerson.Name)
            builder.Append(" |  ")
            builder.Append(currentPerson.FirstName)
            builder.Append(" |  ")
            builder.Append(currentPerson.DateOfBirth.ToString("yyyy-MM-dd"))
            builder.Append(" |  ")
            builder.Append(currentPerson.DateOfEmployment.ToString("yyyy-MM-dd"))
            builder.Append(vbCrLf)
        Next

        MsgBox(builder.ToString(), , title)
    End Sub

    Public ReadOnly Property Count() As Integer Implements System.Collections.ICollection.Count

        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 ArgumentOutOfRangeException("index", index, "The index must be greater than or equal to zero and less than the number of items in the PersonCollection")
            End If

            Return DirectCast(_people(index), Person)
        End Get

    End Property

    Public Sub Add(ByVal thePerson As Person)
        _people.Add(thePerson)
    End Sub

    Public Function GetEnumerator() As System.Collections.IEnumerator _
                                    Implements System.Collections.IEnumerable.GetEnumerator
        Return New PersonCollectionEnumerator(Me)
    End Function

    Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo
        Call _people.CopyTo(array, index)
    End Sub

    Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized
        Get
            Return _people.IsSynchronized
        End Get
    End Property

    Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot
        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.IEnumerator.Current

            Get
                Return _personCol.Item(_index)
            End Get

        End Property

        Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
            _index += 1
            Return (_index < _personCol.Count)
        End Function

        Public Sub Reset() Implements System.Collections.IEnumerator.Reset
            _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.ComparableField.Name)
        _people.Display("Sorted  By Name")
        _people.Sort(Person.ComparableField.DateOfBirth)
        _people.Display("Sorted By DOB")
    End Sub

    Shared Sub Main()
        Call New Tester().TestPeople()
    End Sub

End Class
0
 
bisonfur37Commented:
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(String.Empty)

        builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "----------------------------------------------------" & vbCrLf)

        For Each currentPerson As Person In Me
            builder.Append(currentPerson.Name)
            builder.Append(" |  ")
            builder.Append(currentPerson.FirstName)
            builder.Append(" |  ")
            builder.Append(currentPerson.DateOfBirth.ToString("yyyy-MM-dd"))
            builder.Append(" |  ")
            builder.Append(currentPerson.DateOfEmployment.ToString("yyyy-MM-dd"))
            builder.Append(vbCrLf)
        Next

        Dim writer As New System.IO.StreamWriter(path)
        writer.Write(builder.ToString())
        writer.Close()
    End Sub


To  write XML instead of pipe-delimted, check out the  documentation for the XmlWriter or XmlDataDocument classes
0
 
AndyAelbrechtCommented:
'::: to write to XML with bisonfur37's class:

Private xmls as String  'this is the xml scheme file (see below)

Public Sub WriteToXML(ByVal path as String)

        Dim ds As New DataSet()
        ds.ReadXmlSchema(xmls)
        Dim dt As DataTable
        dt = ds.Tables(0)
        Dim values(3) As String

        For Each currentPerson As Person In Me
            values(0) = currentPerson.Name
            values(1) = currentPerson.FirstName
            values(2) = currentPerson.DateOfBirth.ToString("yyyy-MM-dd")
            values(3) = currentPerson.DateOfEmployment.ToString("yyyy-MM-dd")
            dt.Rows.Add(values)
        Next

        dt.WriteXml(path, XmlWriteMode.WriteSchema, True)
End Sub

'::: the XSD file (this file does not have to be this complicated, but it'll provide a very nice XML output file)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DocumentElement" targetNamespace="users_namespace" xmlns:mstns="users_namespace" xmlns="users_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="DocumentElement" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="users">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string" minOccurs="0" />
              <xs:element name="FirstName" type="xs:string" minOccurs="0" />
              <xs:element name="DoB" type="xs:string" minOccurs="0" />
              <xs:element name="DoE" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
keessefAuthor Commented:
Thanks both for helping me much further.Three questions still (hope you can help)

1 .I've got to do the following:

I must sort the persons into 5 categories:

-15 till 24 years
-25 till 34 years
-35 till 44 years
-45 till 54 years
-55 years and older

The output must be sorted in those 5 categories and it must be sorted per category by date of employment (the last one first..)

2.

How can i display the array in a label? I've got two boxes on the form and a button. When clicked it does this:
 
_people.Add(New Person(txtName.Text, txtFirstname.Text, DateSerial(1930, 1, 1), DateSerial(2006, 3, 12)))

How can i display on the form the whole array (_people) in a label. So everytime  a new person is added there is one extra in the label.

3.

I've got then the whole output in the array in a label (is this the best way?). I want to be able to modify the data in the label so.. i give an example

John Doe John 24-10-1999 26-12-1981
Jim Tayler Jim 1-01-2000 22-08-1965

If i want to modify Jim, i want able to click on it and the data is displayed back in my form fields.

Many Thanks

Frank
0
 
bisonfur37Commented:
2-3 Use a listbox or  listview control instead of a label (http://www.startvbdotnet.com/controls/listbox.aspx)


1. Your new requirements are moving you closer to needing more advanced external querying capabilities (e.g. SQL. XQuery).  However, using strictly VB.NET I did satisfy your requirements (albeit in a  sloppy, non-extensible way) below:

    Public Class PersonComparer
        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            If Not (TypeOf x Is Person) Then Throw New ArgumentException("Invalid comparison", "x")
            Return DirectCast(x, Person).CompareTo(y)
        End Function
    End Class

    Public Class Person
        Implements IComparable

        Public Enum ComparableField
            Name
            FirstName
            DateOfBirth
            DateOfEmployment
            AgeGroupPlusDateOfEmployment
        End Enum

        Private _name As String
        Private _firstName As String
        Private _dob As DateTime
        Private _doe As DateTime
        Private Shared _comparisonField As ComparableField

        Public ReadOnly Property Age() As Long

            Get
                Dim now As DateTime = DateTime.Now
                Dim toReturn As Long = DateDiff(DateInterval.Year, Me.DateOfBirth, now)
                If DateSerial(now.Year, now.Month, now.Day) < DateSerial(now.Year, DateOfBirth.Month, DateOfBirth.Day) Then toReturn -= 1
                Return toReturn
            End Get

        End Property

        Private ReadOnly Property AgeGroup() As String
            Get

                Select Case Me.Age
                    Case 15 - 24
                        Return "A"
                    Case 25 - 34
                        Return "B"
                    Case 35 - 44
                        Return "C"
                    Case 45 - 54
                        Return "D"
                    Case 55 - 1000
                        Return "E"

                End Select

            End Get
        End Property

        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.CompareTo
            If Not TypeOf obj Is Person Then Throw New ArgumentException("Invalid comparison", "obj")
            Dim otherPerson As Person = obj

            'ToDo: create CompareTo delegate function and create a PersonCompareToFactory class
            Select Case _comparisonField

                Case ComparableField.DateOfBirth
                    Return Me.DateOfBirth.CompareTo(otherPerson.DateOfBirth)

                Case ComparableField.DateOfEmployment
                    Return Me.DateOfEmployment.CompareTo(otherPerson.DateOfEmployment)

                Case ComparableField.FirstName
                    Return Me.FirstName.CompareTo(otherPerson.FirstName)

                Case ComparableField.Name
                    Return Me.Name.CompareTo(otherPerson.Name)

                Case ComparableField.AgeGroupPlusDateOfEmployment

                    If Me.AgeGroup = otherPerson.AgeGroup Then
                        Return Me.DateOfEmployment.CompareTo(otherPerson.DateOfEmployment)

                    Else
                        Return Me.AgeGroup.CompareTo(otherPerson.AgeGroup)

                    End If

            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(String.Empty)

            builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "----------------------------------------------------" & vbCrLf)

            For Each currentPerson As Person In Me
                builder.Append(currentPerson.Name)
                builder.Append(" |  ")
                builder.Append(currentPerson.FirstName)
                builder.Append(" |  ")
                builder.Append(currentPerson.DateOfBirth.ToString("yyyy-MM-dd"))
                builder.Append(" |  ")
                builder.Append(currentPerson.DateOfEmployment.ToString("yyyy-MM-dd"))
                builder.Append(vbCrLf)
            Next

            MsgBox(builder.ToString(), , title)
        End Sub

        Public ReadOnly Property Count() As Integer Implements System.Collections.ICollection.Count

            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 ArgumentOutOfRangeException("index", index, "The index must be greater than or equal to zero and less than the number of items in the PersonCollection")
                End If

                Return DirectCast(_people(index), Person)
            End Get

        End Property

        Public Sub Add(ByVal thePerson As Person)
            _people.Add(thePerson)
        End Sub

        Public Function GetEnumerator() As System.Collections.IEnumerator _
                                        Implements System.Collections.IEnumerable.GetEnumerator
            Return New PersonCollectionEnumerator(Me)
        End Function

        Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo
            Call _people.CopyTo(array, index)
        End Sub

        Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized
            Get
                Return _people.IsSynchronized
            End Get
        End Property

        Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot
            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.IEnumerator.Current

                Get
                    Return _personCol.Item(_index)
                End Get

            End Property

            Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
                _index += 1
                Return (_index < _personCol.Count)
            End Function

            Public Sub Reset() Implements System.Collections.IEnumerator.Reset
                _index = -1
            End Sub

        End Class

        Public Sub WriteToFile(ByVal path As String)
            Dim builder As New System.Text.StringBuilder(String.Empty)

            builder.Append("Name | FirstName | DOB | DOE " & vbCrLf & "----------------------------------------------------" & vbCrLf)

            For Each currentPerson As Person In Me
                builder.Append(currentPerson.Name)
                builder.Append(" |  ")
                builder.Append(currentPerson.FirstName)
                builder.Append(" |  ")
                builder.Append(currentPerson.DateOfBirth.ToString("yyyy-MM-dd"))
                builder.Append(" |  ")
                builder.Append(currentPerson.DateOfEmployment.ToString("yyyy-MM-dd"))
                builder.Append(vbCrLf)
            Next

            Dim writer As New System.IO.StreamWriter(path)
            writer.Write(builder.ToString())
            writer.Close()
        End Sub

    End Class

    Public Class Tester
        Private _people As New PersonCollection

        Public Sub TestPeople()
            _people.Add(New Person("Harold Zoid", "Bender", DateSerial(1930, 8, 8), DateSerial(2006, 4, 14)))
            _people.Add(New Person("John Doe", "John", DateSerial(1990, 1, 1), DateSerial(2006, 3, 12)))
            _people.Add(New Person("Homer Simpson", "Homer", DateSerial(1989, 3, 1), DateSerial(2004, 2, 12)))
            _people.Add(New Person("Jane Does", "Jane", DateSerial(1989, 8, 8), DateSerial(2002, 3, 20)))
            _people.Add(New Person("Bender Bending Rodriguez", "Bender", DateSerial(1990, 8, 8), DateSerial(2001, 8, 8)))


            _people.Sort(Person.ComparableField.AgeGroupPlusDateOfEmployment)
            _people.Display("Sorted  By Age Group, Date OfEmployment")
        End Sub

        Shared Sub Main()
            Call New SortPeople.Tester().TestPeople()
        End Sub

    End Class
0
 
keessefAuthor Commented:
Hi,

Thanks for the answer.. I'm only getting one error messages on this code:

 Private ReadOnly Property AgeGroup() As String
        Get
            Select Case Me.Age
                Case 15 - 24
                    Return "A"
                Case 25 - 34
                    Return "B"
                Case 35 - 44
                    Return "C"
                Case 45 - 54
                    Return "D"
                Case 55 - 1000
                    Return "E"

            End Select

        End Get
    End Property

Warning      1      Property 'AgeGroup' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.      

Help! ;-)

Regards,

Frank
0
 
keessefAuthor Commented:
Hi,

Checked it again but the output is also wrong.. I don't no how it's sorted ;-)

I get in the code another error messages:

(this is in the class Tester.vb)

Call New SortPeople.Tester().TestPeople()

Error message:

Error      2      Type 'SortPeople.Tester' is not defined.

Regards,

Frank
0
 
keessefAuthor Commented:
Sorry, Third comment:

If you take out the SortPeople and leave it as Tester().Testpeople() it's working. Don't now if it's correct.

This is my list:
 _people.Add(New Person("Harold Zoid", "Bender", DateSerial(1930, 8, 8), DateSerial(2006, 4, 14)))
        _people.Add(New Person("John Doe", "John", DateSerial(1990, 1, 1), DateSerial(2006, 3, 12)))
        _people.Add(New Person("Homer Simpson", "Homer", DateSerial(1989, 3, 1), DateSerial(2004, 2, 12)))
        _people.Add(New Person("Jane Does", "Jane", DateSerial(1989, 8, 8), DateSerial(2002, 3, 20)))
        _people.Add(New Person("Bender Bending Rodriguez", "Bender", DateSerial(1990, 8, 8), DateSerial(2001, 8, 8)))
        _people.Add(New Person("Bender Bending Marinissen", "Jan-Willem", DateSerial(1931, 8, 8), DateSerial(2003, 3, 8)))
        _people.Add(New Person("Bender Bending Rodriguez", "Joke", DateSerial(1982, 8, 5), DateSerial(2005, 12, 8)))
        _people.Add(New Person("Bender Bending Rodriguez", "Richard", DateSerial(1976, 2, 8), DateSerial(2002, 1, 18)))
        _people.Add(New Person("Bender Bending Rodriguez", "Frank", DateSerial(1975, 3, 8), DateSerial(1988, 2, 21)))

This is the output now:

Bender Bending Rodriguez |  Frank |  1975-03-08 |  1988-02-21
Bender Bending Rodriguez |  Bender |  1990-08-08 |  2001-08-08
Bender Bending Rodriguez |  Richard |  1976-02-08 |  2002-01-18
Jane Does |  Jane |  1989-08-08 |  2002-03-20
Bender Bending Marinissen |  Jan-Willem |  1931-08-08 |  2003-03-08
Homer Simpson |  Homer |  1989-03-01 |  2004-02-12
Bender Bending Rodriguez |  Joke |  1982-08-05 |  2005-12-08
John Doe |  John |  1990-01-01 |  2006-03-12
Harold Zoid |  Bender |  1930-08-08 |  2006-04-14


Question number 2

How can i access in the code the categories: So display only the people in the categorie of: 45-54 years.


Question number 3

I found this in the code:

   Public ReadOnly Property Age() As Long

        Get
            Dim now As DateTime = DateTime.Now
            Dim toReturn As Long = DateDiff(DateInterval.Year, Me.DateOfBirth, now)
            If DateSerial(now.Year, now.Month, now.Day) < DateSerial(Now.Year, DateOfBirth.Month, DateOfBirth.Day) Then toReturn -= 1
            Return toReturn
        End Get

    End Property

Original line:

     If DateSerial(now.Year, now.Month, now.Day) < DateSerial(Now.Year, DateOfBirth.Month, DateOfBirth.Day) Then toReturn -= 1

Shouldn't this be:
                                                                                             
  If DateSerial(now.Year, now.Month, now.Day) < DateSerial(DateOfBirth.Year, DateOfBirth.Month, DateOfBirth.Day) Then toReturn -= 1

Thanks again for your help!!! Happy easter!

Regards,

Frank
0
 
bisonfur37Commented:
1.  To remove the warning, change "Case 55-1000" to "Case Else"

2.  My classes existed in  namespace "SortPeople",  which you correctly removed from the function call

3.  OK... found a major bug that causes incorrect sorting  --> in property AgeGroup, replace the "-" character with the word  "To" (e.g. "Case 55 - 1000" becomes "Case 55 To  1000"). Sorry for the careless mistake

4. First you  should change the access modifier for  property AgeGroup  from  "Private"  to "Public." That will allow you to access the property as normal such as "_people(0).AgeGroup". Note that this will property returns a string representing the person's Age  Group as defined in the code (I recommend making these constants). To access only people  in  a certain age group, loop through the collection and select only those in the age group:

            For Each currentPerson As Person In _people

                If currentPerson.AgeGroup = "A" Then
                    'logic  for people aged 15-24
                End If

            Next


5. Although it looks odd, the code for the Age property is actually (pretty much) correct. The problem is that the built-in DateDiff method will always "round up" the number of years between the two date paramaeters, so that if you were born on 2005-12-31 and today is 2006-04-16, DateDiff will  return 1.  The line of code in question adjusts for this  by subtracting one from the return value if the person's birthday has not occurred yet in the current year. The  logic is not precise --  for example, DateSerial(Now.Year, DateOfBirth.Month, DateOfBirth.Day) can throw an error if DateOfBirth.Month = February, DateOfBirth.Day = 29, and Now.Year is  not a leap year. But  I will  leave it as an exercise for you to clean up the  logic...
0
 
keessefAuthor Commented:
Thanks!! Great it works!

One small question before I rewared you the points; Date of Employment must be sorted on the people who where last employed..(so last one's employed first).... (Sorry ;-) )

Thanks for your help again!! Really good!

Regards,

Frank
0
 
bisonfur37Commented:
assuming you want to sort Date of Employment (DOE) descending when sorting by either just DOE or DOE plus Age Range,  change method Person.CompareTo to the following:

        Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
            If Not TypeOf obj Is Person Then Throw New ArgumentException("Invalid comparison", "obj")
            Dim otherPerson As Person = obj

            'ToDo: create CompareTo delegate function and create a PersonCompareToFactory class
            Select Case _comparisonField

                Case ComparableField.DateOfBirth
                    Return Me.DateOfBirth.CompareTo(otherPerson.DateOfBirth)

                Case ComparableField.DateOfEmployment
                    'Return Me.DateOfEmployment.CompareTo(otherPerson.DateOfEmployment)
                    Return otherPerson.DateOfEmployment.CompareTo(Me.DateOfEmployment)

                Case ComparableField.FirstName
                    Return Me.FirstName.CompareTo(otherPerson.FirstName)

                Case ComparableField.Name
                    Return Me.Name.CompareTo(otherPerson.Name)

                Case ComparableField.AgeGroupPlusDateOfEmployment

                    If Me.AgeGroup = otherPerson.AgeGroup Then
                        'Return Me.DateOfEmployment.CompareTo(otherPerson.DateOfEmployment)
                        Return otherPerson.DateOfEmployment.CompareTo(Me.DateOfEmployment)

                    Else
                        Return Me.AgeGroup.CompareTo(otherPerson.AgeGroup)

                    End If

            End Select

        End Function
0
 
keessefAuthor Commented:
Hi bisonfur37,

Thanks again for helping me out.. I hope you can help me for the last time (i've already rewarded the points)..

I'm trying to search in the array..

I've got the following line:

Dim index as Integer = Array.IndexOf(_person,"John")

This is not going to work.. Can you please provide me with some more detail how i can access the Array.. (sorry i'm still now 2/3 weeks busy with vb.net)..

Thank you very much!

Frank
0
 
bisonfur37Commented:
given the current structure, the easiest way is to just  loop through all elements in the collection:

            Dim index As Integer = -1

            For i As Integer = 0 To _people.Count - 1

                If String.Compare(_people(i).Name, "john", True) = 0 Then
                    index = i
                    Exit For
                End If

            Next
0
 
keessefAuthor Commented:
Hi Bisofur37,

I hope you can shed your light on this:

I'm adding people into the array by:

Example (page2)
_people.Add(New Person("John Doe", "John", DateSerial(1990, 1, 1), DateSerial(2006, 3, 12)))
_people.Sort(Person.ComparableField.AgeGroupPlusDateOfEmployment)

On another page (page 3) i'm doing this:
 Private Sub stap3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each currentPerson As Person In _people

            If currentPerson.AgeGroup = "A" Then
                ListBox1.Items.Add(currentPerson.Name)
            End If
            If currentPerson.AgeGroup = "B" Then
                ListBox2.Items.Add(currentPerson.Name)
            End If
            If currentPerson.AgeGroup = "C" Then
                ListBox3.Items.Add(currentPerson.Name)
            End If
            If currentPerson.AgeGroup = "D" Then
                ListBox4.Items.Add(currentPerson.Name)
            End If
            If currentPerson.AgeGroup = "E" Then
                ListBox5.Items.Add(currentPerson.Name)
            End If


        Next
    End Sub

If i'm doing it on the same 'page', adding, sorting and displaying everything works fine.. Can you give me a hint?

Thanks,

Frank
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now