You can implement IComparable in your structure, then a List(Of Score) or a Score() will sort itself.You would continue to nest If statements in that CompareTo method for each step of your countback algorithm. Now you can just call GrossScores.Sort() and everything will be taken care of. That' encapsulation, which is one of the cornerstones of OOP.vb.net Code:
Public Structure Scores Implements IComparable Public Gross() As Integer Public Player As String Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim other As Scores = DirectCast(obj, Scores) 'Compare the total scores first. Dim result As Integer = Me.Gross.Sum().CompareTo(other.Gross.Sum()) If result = 0 Then 'The total scores are the same so perform a countback. result = Me.GetSum(Me.Gross, 9, 17).CompareTo(Me.GetSum(other.Gross, 9, 17)) If result = 0 Then 'The scores for the last 9 holes are the same so continue the countback '... End If End If Return result End Function Private Function GetSum(ByVal gross As Integer(), _ ByVal startIndex As Integer, _ ByVal endIndex As Integer) As Integer Dim sum As Integer = 0 For index As Integer = startIndex To endIndex sum += gross(index) Next Return sum End Function End Structure




Reply With Quote