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
Do
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.DeleteFile("previous.txt")
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.Close()
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
allLines.Add(reader.ReadLine())
Loop
reader.Close()
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
End Sub
End Class