Hey all,

I'm new to VB.NET, and to programming altogether actually, and I need some help. I am making a program which stores the names and test scores of five students collected from input boxes into a two dimensional array; displays the data in a list box (student names in one column and their corresponding scores on the other); and displays the names and scores of the two highest scoring students in a second list box. The design is made up of two list boxes and three buttons. The first button prompts the user to enter the names and scores, the second displays the input, and the third displays the top two students and scores. I am using StreamWriter and StreamReader to transcribe the data stored in the array onto the list box with the second button, but I have absolutely no clue how to sort this data according to score. This is the code that I have written so far.

Public Class Form1
    Dim fmtstr As String = "{0,-20}     {1,4}"
    Dim userInput(4, 1), strName_Score(4, 1) As String


    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim i As Integer
        Dim intNum As Integer = i + 1

        'Enter data into input box and store into array
        For i = 0 To 4

            userInput(0, 0) = "Enter student 1's name."
            userInput(0, 1) = "Enter student 1's score."
            userInput(1, 0) = "Enter student 2's name."
            userInput(1, 1) = "Enter student 2's score."
            userInput(2, 0) = "Enter student 3's name."
            userInput(2, 1) = "Enter student 3's score."
            userInput(3, 0) = "Enter student 4's name."
            userInput(3, 1) = "Enter student 4's score."
            userInput(4, 0) = "Enter student 5's name."
            userInput(4, 1) = "Enter student 5's score."

            strName_Score(i, 0) = InputBox(userInput(i, 0), "Input Name")
            strName_Score(i, 1) = InputBox(userInput(i, 1), "Input Score")
        Next

        'Store array as .txt file
        Dim sw As IO.StreamWriter = IO.File.CreateText("StudentScores.txt")
        For i = 0 To 4
            sw.WriteLine(strName_Score(i, 0))
            sw.WriteLine(strName_Score(i, 1))
        Next
        sw.Close()
    End Sub

    Private Sub btnInputData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputData.Click
        'Read .txt file and display input in list box
        Dim sr As IO.StreamReader = IO.File.OpenText("StudentScores.txt")
        dispInput.Items.Clear()
        Do While sr.Peek <> -1
            dispInput.Items.Add(String.Format(fmtstr, sr.ReadLine, sr.ReadLine))
        Loop
    End Sub
End Class

What do I do from here? Please explain, I want to learn as much as I can. Also, please let me know if there is anything I could have done or can still do to accomplish this task more efficiently. Thanks so much.

Get the scores into an array, and use array.sort()
You could also use List

Get the scores into an array, and use array.sort()
You could also use List

Stares blankly at the screen....

lol I'm sorry, how exactly do I do this? Also, what benefits are there to using array.sort() as opposed to List and vice versa?

Member Avatar for Unhnd_Exception

List(Of T)

Create a Class named NameScore.

Hold the objects in a List(Of NameScore)

Create a Class that implements IComparer(Of T)

Sort the list with the new created class that implements IComparer(Of NameScore)

Code Example

Public Class Form1

    'Create a class to hold the name and scores.
    Private Class NameScore
        Private fpName As String
        Private fpScore As Integer

        'Implement the IComparer of T to handle the sorting of
        'the list.
        Class Comparer : Implements IComparer(Of NameScore)

            Public Function Compare(ByVal x As NameScore, ByVal y As NameScore) As Integer Implements IComparer(Of NameScore).Compare

                If x.Score > y.Score Then
                    Return 1 'For descending return -1
                ElseIf x.Score = y.Score Then
                    Return 0
                Else
                    Return -1 'For descending return 1
                End If

            End Function

        End Class

        Public Property Name() As String
            Get
                Return fpName
            End Get
            Set(ByVal value As String)
                fpName = value
            End Set
        End Property

        Public Property Score() As Integer
            Get
                Return fpScore
            End Get
            Set(ByVal value As Integer)
                fpScore = value
            End Set
        End Property

        Sub New()

        End Sub

        Sub New(ByVal name As String, ByVal score As Integer)
            fpName = name
            fpScore = score
        End Sub

    End Class

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnter.Click

        'Create a list of T to store the name and scores.
        Dim AllScores As New List(Of NameScore)
        Dim Name As String
        Dim Score As Integer

        'Enter data into input box and store into array
        For i = 0 To 1

            Name = InputBox("Enter student " & i + 1 & "'s name.", "Input Name")
            Score = InputBox("Enter student " & i + 1 & "'s score.", "Input Score")

            'Validation Required for Name and Score
            AllScores.Add(New NameScore(Name, Score))
        Next

        'Sort the list with the Icomparer created in the 
        'NameScore Class.
        AllScores.Sort(New NameScore.Comparer)

        'Sorted

        'Store array as .txt file
        Dim sw As IO.StreamWriter = IO.File.CreateText("StudentScores.txt")
        For i = 0 To AllScores.Count - 1
            sw.WriteLine(AllScores(i).Name)
            sw.WriteLine(AllScores(i).Score)
        Next
        sw.Close()

    End Sub

    Private Sub btnInputData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonInputData.Click
        'Read .txt file and display input in list box
        Dim sr As IO.StreamReader = IO.File.OpenText("StudentScores.txt")
        Dim fmtstr As String = "{0,-20}     {1,4}"
        dispInput.Items.Clear()
        Do While sr.Peek <> -1
            dispInput.Items.Add(String.Format(fmtstr, sr.ReadLine, sr.ReadLine))
        Loop
    End Sub

End Class

By the way Array.Sort will only sort a 1 demensional array. It can be done however. If your hell bent on using a 2 demension array I can show you. I would recommend the List(Of T) as above.

commented: Very informative and helpful +1

Thanks a lot; I am definitely not "hell bent" on doing it any particular way. I'll give it a shot. With this method, how then would I code for a third button "display two highest scores" to display the names and scores of only the top two in a second listbox?

Also, could you please explain the purpose of lines 44-46?

Sub New()
 
        End Sub
Member Avatar for Unhnd_Exception

Its not required. You can remove it but you would have to pass parameters to the object everytime you initialized it.

You may just want to intialize a class with no parameters.

Dim NS as New NameScore
NS.Name = "triple_A"
Ns.Score = 3

Thats all.

Its always good to have a parameterless constructor.

You may find yourself in a situation where you create a class that requires a crucial piece of information for it to work. In that case you would omit the parameterless New constructor and require that piece of information to be passed in to the constructor. With the NameScore class it doesn't really matter. It makes it nice to be able to inialize in 1 line of code instead of 3 lines of code like above.

Understood, thanks so much.

Ok, here is code. It works perfectly, but I am wondering if there is any way to have the scores that are displayed in the listboxes aligned regardless of the length of the students' names?

Also, how do I make it so a message box is displayed if the wrong sort of data is entered into the input box? It just crashes if that happens as the code is currently written. Sorry for all the questions, I'm new after all.

Public Class Form1

    Private Class NameScore
        Private fpName As String
        Private fpScore As Integer

        Class Comparer : Implements IComparer(Of NameScore)

            Public Function Compare(ByVal x As NameScore, ByVal y As NameScore) As Integer Implements IComparer(Of NameScore).Compare

                If x.Score > y.Score Then
                    Return -1
                ElseIf x.Score = y.Score Then
                    Return 0
                Else
                    Return 1
                End If

            End Function

        End Class

        Public Property Name() As String
            Get
                Return fpName
            End Get
            Set(ByVal value As String)
                fpName = value
            End Set
        End Property

        Public Property Score() As Integer
            Get
                Return fpScore
            End Get
            Set(ByVal value As Integer)
                fpScore = value
            End Set
        End Property

        Sub New()

        End Sub

        Sub New(ByVal name As String, ByVal score As Integer)
            fpName = name
            fpScore = score
        End Sub

    End Class

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        'Create a list to store the name and scores.
        Dim AllScores As New List(Of NameScore)
        Dim Name As String
        Dim Score As Integer

        'Enter data into input box and store into array
        For i = 0 To 4

            Name = InputBox("Enter student " & i + 1 & "'s name.", "Input Name")
            Score = InputBox("Enter student " & i + 1 & "'s score.", "Input Score")

            AllScores.Add(New NameScore(Name, Score))
        Next

        'Store array as .txt file
        Dim sw As IO.StreamWriter = IO.File.CreateText("StudentScores.txt")
        For i = 0 To AllScores.Count - 1
            sw.WriteLine(AllScores(i).Name)
            sw.WriteLine(AllScores(i).Score)
        Next
        sw.Close()

        'Sort the list 
        AllScores.Sort(New NameScore.Comparer)

        'Store sorted array as .txt file
        Dim sw2 As IO.StreamWriter = IO.File.CreateText("TopScores.txt")
        For i = 0 To 1
            sw2.WriteLine(AllScores(i).Name)
            sw2.WriteLine(AllScores(i).Score)
        Next
        sw2.Close()

    End Sub

    Private Sub btnInputData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputData.Click

        'Read .txt file and display input in list box
        Dim sr As IO.StreamReader = IO.File.OpenText("StudentScores.txt")
        Dim fmtstr As String = "{0,-20}     {1, 4}"
        Do While sr.Peek <> -1
            dispInput.Items.Add(String.Format(fmtstr, sr.ReadLine, sr.ReadLine))
        Loop

    End Sub

    Private Sub btnTwoHighest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwoHighest.Click

        'Read .txt file and display output in list box
        Dim sr As IO.StreamReader = IO.File.OpenText("TopScores.txt")
        Dim fmtstr As String = "{0,-20}     {1, 4}"
        dispOutput.Items.Clear()
        Do While sr.Peek <> -1
            dispOutput.Items.Add(String.Format(fmtstr, sr.ReadLine, sr.ReadLine))
        Loop

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.