Demo Code
Both demo implement a small team league table sorting method.
It shows how to store the same information in both parallel arrays and using objects.
Option Strict On
Module Module1
Sub Main()
ParallelArray.Demo()
' ClassBased.Demo()
Console.ReadKey()
End Sub
<Runtime.CompilerServices.Extension()>
Public Sub SwapWith(Of T)(ByRef ThisObj As T,
ByRef WithThis As T)
Dim tmp As T = ThisObj
ThisObj = WithThis
WithThis = tmp
End Sub
End Module
Parallel Arrays
Public Module ParallelArray
Public Sub Demo()
Dim team_Names() As String = {"A", "B", "C", "D", "E"}
Dim team_Wins() As Integer = {2, 3, 1, 0, 0}
Dim team_Draw() As Integer = {0, 0, 1, 2, 2}
Dim team_Lose() As Integer = {0, 0, 0, 0, 0}
Dim team_GF() As Integer = {1, 5, 3, 1, 4}
Dim team_GA() As Integer = {1, 2, 4, 5, 6}
Console.WriteLine("Using Parallel Arrays")
Console.WriteLine("Before Sorting")
For i = 0 To 4
Console.WriteLine("{0} {1} {2} {3} {4} {5}", team_Names(i), team_Wins(i), team_Draw(i), team_Lose(i), team_GF(i), team_GA(i))
Next
'
' Simple Bubble Sort
'
For o = 0 To 3
For i = o + 1 To 4
If team_Wins(o) < team_Wins(i) Then
' Swap Places
team_Names(o).SwapWith(team_Names(i))
team_Wins(o).SwapWith(team_Wins(i))
team_Draw(o).SwapWith(team_Draw(i))
team_Lose(o).SwapWith(team_Lose(i))
team_GF(o).SwapWith(team_GF(i))
team_GA(o).SwapWith(team_GA(i))
ElseIf team_Wins(o) = team_Wins(i) Then
If team_Draw(o) < team_Draw(i) Then
' Swap Places
team_Names(o).SwapWith(team_Names(i))
team_Wins(o).SwapWith(team_Wins(i))
team_Draw(o).SwapWith(team_Draw(i))
team_Lose(o).SwapWith(team_Lose(i))
team_GF(o).SwapWith(team_GF(i))
team_GA(o).SwapWith(team_GA(i))
ElseIf team_Draw(o) = team_Draw(i) Then
If team_Lose(o) > team_Lose(i) Then
' Swap Places
team_Names(o).SwapWith(team_Names(i))
team_Wins(o).SwapWith(team_Wins(i))
team_Draw(o).SwapWith(team_Draw(i))
team_Lose(o).SwapWith(team_Lose(i))
team_GF(o).SwapWith(team_GF(i))
team_GA(o).SwapWith(team_GA(i))
ElseIf team_Lose(o) = team_Lose(i) Then
' Goal Diffference
If (team_GF(o) - team_GA(o)) < (team_GF(i) - team_GA(i)) Then
' Swap Places
team_Names(o).SwapWith(team_Names(i))
team_Wins(o).SwapWith(team_Wins(i))
team_Draw(o).SwapWith(team_Draw(i))
team_Lose(o).SwapWith(team_Lose(i))
team_GF(o).SwapWith(team_GF(i))
team_GA(o).SwapWith(team_GA(i))
ElseIf (team_GF(o) - team_GA(o)) = (team_GF(i) - team_GA(i)) Then
If team_GF(o) < team_GF(i) Then
' Swap Places
team_Names(o).SwapWith(team_Names(i))
team_Wins(o).SwapWith(team_Wins(i))
team_Draw(o).SwapWith(team_Draw(i))
team_Lose(o).SwapWith(team_Lose(i))
team_GF(o).SwapWith(team_GF(i))
team_GA(o).SwapWith(team_GA(i))
End If
End If
End If
End If
End If
Next
Next
Console.WriteLine()
Console.WriteLine("After Sorting")
For i = 0 To 4
Console.WriteLine("{0} {1} {2} {3} {4} {5}", team_Names(i), team_Wins(i), team_Draw(i), team_Lose(i), team_GF(i), team_GA(i))
Next
End Sub
End Module
Now let's look at some task but using an array with objects.
Array with Objects (Class & Structure)
The creation of the class object.
Don't worry if you don't fully understand everything I'll provide links to other tutorials.
Public Module ClassBased
Public Class Team
Implements IComparable(Of Team)
Implements IComparer(Of Team)
Protected _Name As String = ""
Protected _Wins As Integer = 0
Protected _Draws As Integer = 0
Protected _Lose As Integer = 0
Protected _GF As Integer = 0
Protected _GA As Integer = 0
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Wins As Integer
Get
Return _Wins
End Get
Set(ByVal value As Integer)
_Wins = value
End Set
End Property
Public Property Draws As Integer
Get
Return _Draws
End Get
Set(ByVal value As Integer)
_Draws = value
End Set
End Property
Public Property Loses As Integer
Get
Return _Lose
End Get
Set(ByVal value As Integer)
_Lose = value
End Set
End Property
Public Property GF As Integer
Get
Return _GF
End Get
Set(ByVal value As Integer)
_GF = value
End Set
End Property
Public Property GA As Integer
Get
Return _GA
End Get
Set(ByVal value As Integer)
_GA = value
End Set
End Property
Public Sub New()
_Name = ""
_Wins = 0
_Draws = 0
_Lose = 0
_GF = 0
_GA = 0
End Sub
#Region "Extras"
#Region "Overload for Sub New"
Public Sub New(ByVal Name As String,
ByVal Wins As Integer,
ByVal Draws As Integer,
ByVal Lose As Integer,
ByVal GF As Integer,
ByVal GA As Integer)
_Name = Name
_Wins = Wins
_Draws = Draws
_Lose = Lose
_GF = GF
_GA = GA
End Sub
#End Region
#Region "Read Only Property for goal difference"
Public ReadOnly Property GD() As Integer
Get
Return _GF - _GA
End Get
End Property
#End Region
#Region "Overloading the ToString Function"
Public Overrides Function ToString() As String
Return String.Format("{0} {1} {2} {3} {4} {5}", _Name, _Wins, _Draws, _Lose, _GF, _GA)
End Function
#End Region
#End Region
Public Function CompareTo(ByVal other As Team) As Integer Implements System.IComparable(Of Team).CompareTo
If Me.Wins < other.Wins Then
' Swap Places
Return 1
ElseIf Me.Wins = other.Wins Then
If Me.Draws < other.Draws Then
Return 1
ElseIf Me.Draws = other.Draws Then
If Me.Loses > other.Loses Then
Return 1
ElseIf Me.Loses = other.Loses Then
' Goal Diffference
If Me.GD < other.GD Then
Return 1
ElseIf Me.GD = other.GD Then
If Me._GF < other._GF Then
Return 1
Else
Return 0
End If
End If
End If
End If
End If
Return -1
End Function
Public Function Compare(ByVal x As Team, ByVal y As Team) As Integer Implements System.Collections.Generic.IComparer(Of Team).Compare
Return x.CompareTo(y)
End Function
End Class
Now let's look at how to do the task.
Public Sub Demo()
Dim team_Names() As String = {"A", "B", "C", "D", "E"}
Dim team_Wins() As Integer = {2, 3, 1, 0, 0}
Dim team_Draw() As Integer = {0, 0, 1, 2, 2}
Dim team_Lose() As Integer = {0, 0, 0, 0, 0}
Dim team_GF() As Integer = {1, 5, 3, 1, 4}
Dim team_GA() As Integer = {1, 2, 4, 5, 6}
Dim teams() As Team = {New Team("A", 2, 0, 0, 1, 1),
New Team("B", 3, 0, 0, 5, 2),
New Team("C", 1, 1, 0, 3, 4),
New Team("D", 0, 2, 0, 1, 5),
New Team("E", 0, 2, 0, 4, 6)}
Console.WriteLine("Using Class Based Arrays")
Console.WriteLine("Before Sorting")
For i = 0 To 4
Console.WriteLine("{0} {1} {2} {3} {4} {5}", teams(i).Name, teams(i).Wins, teams(i).Draws, teams(i).Loses, teams(i).GF, teams(i).GA)
Next
Console.WriteLine("")
Console.WriteLine("Using Overload ToString")
Console.WriteLine("")
For Each Team In teams
Console.WriteLine(Team.ToString())
Next
Console.WriteLine()
The follow commented out code does the bubble sort, but this time using the Team Class.
''
'' Simple Bubble Sort
''
'For o = 0 To 3
' For i = o + 1 To 4
' If teams(o).Wins < teams(i).Wins Then
' ' Swap Places
' teams(o).SwapWith(teams(i))
' ElseIf team_Wins(o) = team_Wins(i) Then
' If team_Draw(o) < team_Draw(i) Then
' ' Swap Places
' teams(o).SwapWith(teams(i))
' ElseIf team_Draw(o) = team_Draw(i) Then
' If team_Lose(o) > team_Lose(i) Then
' ' Swap Places
' teams(o).SwapWith(teams(i))
' ElseIf team_Lose(o) = team_Lose(i) Then
' ' Goal Diffference
' If teams(o).GD < teams(i).GD Then
' ' Swap Places
' teams(o).SwapWith(teams(i))
' ElseIf teams(o).GD = teams(i).GD Then
' If team_GF(o) < team_GF(i) Then
' ' Swap Places
' teams(o).SwapWith(teams(i))
' End If
' End If
' End If
' End If
' End If
' Next
'Next
Instead of using the comment of code, lets use the in built functionality of the framework.
Array.Sort(teams, DirectCast(teams(0), IComparer(Of Team)))
Spoiler
Console.WriteLine()
Console.WriteLine("After Sorting")
For Each Team In teams
Console.WriteLine(Team.ToString())
Next
End Sub
End Module
Conclusions
The Parallel Array based method of doing the task is large and complex.
Even I made error whilst writing the code.
The Array with Objects based method of doing the task, it separated the code in discreet encapsulations.
One part stores the data for each team, another does the comparison between two teams.
Overall the code is easier to understand and maintain.
Further Resources
Custom Sorting
Classes Explained
Extension Methods (The SwapWith Method)






MultiQuote


|