Recently, I have been working on games in my programming class. In this class I am farther ahead then most other students, and so, must rely on the internet for help instead of my teacher. I have been working on a highscore counter between runs that I should be able to use in all of my games. So far, I have it so it works the first time without any errors but the second time it says that I am outside the index in the line Return Lines(Line number - 1). I am not sure how to fix this error. In this program Button1 would be activated every time a new score is entered and button 6 is the button that is used to end the program. I do not know if I am overcomplicating my highscore system and in reality it is a really simple thing to code. If anyone can help me fix this, PLEASE HELP. Also, if anyone knows how to simplify this, that would be very much appreciated. If possible, I would like to stick to using text files, because I do not have much experience with any other kind.
This is my code so far.

Public Class Form1
    Dim FName() As String
    Dim Age(2) As Integer
    Dim path As String
    Dim Tb As String
    Dim x As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Computer.FileSystem.FileExists("previous.txt") Then
                Label2.Text = My.Computer.FileSystem.ReadAllText("previous.txt")
            Loop Until Label2.Text = My.Computer.FileSystem.ReadAllText("previous.txt")
        Else : My.Computer.FileSystem.WriteAllText("previous.txt", "", True)
        End If
        My.Computer.FileSystem.WriteAllText("x.txt", "0", True)
        My.Computer.FileSystem.WriteAllText("Numbers.txt", Environment.NewLine + "0", True)
        If Val(ReadLastLineOfFile("x.txt")) = 0 Then My.Computer.FileSystem.WriteAllText("x.txt", Environment.NewLine + "3", True)
        x = Val(ReadLastLineOfFile("x.txt"))
        Dim strLastLine As String
        strLastLine = ReadLastLineOfFile("x.txt")
    End Sub

    Function ReadLastLineOfFile(ByVal sFileName As String) As String
        Dim objFSO, TS
        Dim sTmpContents As String
        objFSO = CreateObject("Scripting.FileSystemObject")
        TS = objFSO.OpenTextFile("x.txt", 1)
        sTmpContents = TS.ReadAll
        TS = Nothing
        objFSO = Nothing
        ReadLastLineOfFile = Split(sTmpContents, vbCrLf)(UBound(Split(sTmpContents, vbCrLf)))
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        My.Computer.FileSystem.WriteAllText("Numbers.txt", Environment.NewLine + TextBox1.Text, True)
        Dim reader As New System.IO.StreamReader("Numbers.txt")
        Dim allLines As List(Of String) = New List(Of String)
        Do While Not reader.EndOfStream
        If Val(ReadLine(x, allLines)) > Val(Label2.Text) Then Label2.Text = ReadLine(x, allLines)
        x = x + 1
    End Sub
    Public Function ReadLine(ByVal lineNumber As Integer, ByVal lines As List(Of String)) As String
        Return lines(lineNumber - 1)
    End Function

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        My.Computer.FileSystem.WriteAllText("x.txt", Environment.NewLine + (x.ToString), True)
        My.Computer.FileSystem.WriteAllText("previous.txt", Environment.NewLine + Label2.Text, True)
    End Sub
End Class
5 Years
Discussion Span
Last Post by schroaus

First off, it is bittersweet to be ahead of your class. Bitter due to having a higher chance to make errors on code that you can't fully understand, but sweet because you can use the time to improve your coding/understanding of the language and logic.

As for this:

   Public Function ReadLine(ByVal lineNumber As Integer, ByVal lines As List(Of String)) As String
       Return lines(lineNumber - 1)
   End Function

If the value of lineNumber is equal to zero or greater than the size of the list + 1 you will always get this error.

You can fix this with something like this:

  Public Function ReadLine(ByVal lineNumber As Integer, ByVal lines As List(Of String)) As String
       If lineNumber - 1 > lines.Count - 1 Then Return "lineNumber was to large for the list: " & (lineNumber - 1) & " > " & lines.Count
       Return lines(lineNumber - 1)
  End Function

If you are trying to retreive the last element in the list, try this:

Public Function ReadLine(ByVal lines As List(Of String)) As String
    If lines.Count <=0 Then 
        Return String.Empty
        Return lines(lines.Count - 1)
    End If
End Function 

This will return the last element of the list, being the last line read in from the text file.

Edited by Begginnerdev


You can also access the last element of a list with the .Last property, lines.Last

Edited by tinstaafl


Here's a simple way to handle high scores. This requires a text file in the debug folder. If you use the Add-New Item to add the text file, then drag it to the debug folder, the file will be included if you use the Publish wizard to deploy your project. The format is one value per line. Populate it with 5 generic values(i.e. 100,150,200,250,300). It's set for top 5, but that's easy to change just change the HiScores array and the values in the file to hold however many scores you want:

Imports System.IO
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Call the sub routine
    End Sub
    Private Sub Do_HiScores(ByVal Score As Integer)
        Dim HiScores(5) As String
        'load the array from the file
        HiScores = (File.ReadAllLines(Application.StartupPath + "\HiScores.txt"))            
        If Score <= HiScores.Min Then
            MsgBox("Sorry your score didn't make the cut")
            If Score >= HiScores.Max Then
                If Score = HiScores.Max Then
                    MsgBox("Congrats you tied the High Score")
                    MsgBox("Congrats you beat the High Score")
                End If
                MsgBox("Congrats you made the High Score list")
            End If
            'replace the lowest score in the array
            HiScores(0) = Str(Score).Trim
            'Sort the array
            'Overwrite the file with the new scores
            File.WriteAllLines(Application.StartupPath + "\HiScores.txt", HiScores)
        End If
    End Sub
End Class

Edited by tinstaafl

Votes + Comments
Simple, yet clean. Nice!

Thank you for the help. I will definately use this code in the future. It is much smaller and easier to create. I found my issue in my own code though. I just forgot to put an Envirenment.Newline in line 16 of my code. The code to read the last line of the text file will deffinately be quicker and easier. Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.