Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Uncomment the following to generate a list of players and scores
'and save them to a file.
'******************************************************************
' Dim allscores As PlayerScore() = GenerateFakeData(300)
' SaveData("C:\PlayerScores.dat", allscores)
'******************************************************************
'Load a list of players and their scores from a binary file.
'Disable this if you have uncommented the code above
'*************************************************************
Dim allscores As PlayerScore() = LoadData("c:\playerscores.dat")
'*************************************************************
Dim topTen As PlayerScore()
'Select only the top ten players after sorting the list in descending order.
'In other words, we are selecting the players with the top 10 scores
topTen = allscores.OrderByDescending(Function(ps) ps.Score).Take(10).ToArray
'Show the top ten players in a ListBox
ListBox1.Items.AddRange(topTen)
End Sub
'Saves the scores of players to a file
Private Sub SaveData(ByVal fileName As String, ByVal scores As IList(Of PlayerScore))
Using fs As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)
Using bw As New BinaryWriter(fs, System.Text.Encoding.UTF8)
bw.Write(scores.Count)
For Each sc As PlayerScore In scores
bw.Write(sc.PlayerName)
bw.Write(sc.Score)
Next
End Using
End Using
End Sub
'Loads the scores of players from file
Private Function LoadData(ByVal fileName As String) As PlayerScore()
Dim playerScores As New List(Of PlayerScore)
Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Using br As New BinaryReader(fs, System.Text.Encoding.UTF8)
For i = 0 To br.ReadInt32 - 1
Dim name As String = br.ReadString
Dim score As Integer = br.ReadInt32
playerScores.Add(New PlayerScore With {.PlayerName = name, .Score = score})
Next
End Using
End Using
Return playerScores.ToArray
End Function
Private Function GenerateFakeData(ByVal numberOfPlayers As Integer) As PlayerScore()
Dim r As New Random
Dim firstNames As String() = New String() {"Kate", "John", "Harry", "Melanie", "Jake", "Johnathan", "Peter", "Alex", "Oliver", "Yohance", "Tyrell", "Peggy", "Irelia", "Kane", "Jimmy"}
Dim lastNames As String() = New String() {"Williams", "Wells", "Blackman", "Jackson", "Whitman", "Stark", "Wheeler", "Livingston"}
Dim playerScores As New List(Of PlayerScore)
For i = 1 To numberOfPlayers
Dim name As String = firstNames(r.Next(0, firstNames.Length)) + " " + lastNames(r.Next(0, lastNames.Length))
Dim score As Integer = r.Next(1, 10 ^ 6)
playerScores.Add(New PlayerScore With {.PlayerName = name, .Score = score})
Next
Return playerScores.ToArray
End Function
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.Location = New System.Drawing.Point(12, 12)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(204, 277)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(242, 312)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
End Class
Public Class PlayerScore
Public Property PlayerName As String
Public Property Score As Integer
Public Overrides Function ToString() As String
Return Me.PlayerName + " " + Score.ToString
End Function
End Class