loading and saving vars using streamreader and streamwriter in VB.NET

  • Thread starter Thread starter Necropony1457
  • Start date Start date
N

Necropony1457

Guest
I'm trying to set up a system to save and load variables from a file using stream reader and stream writer

for saving I have

Public Shared Sub Save(path As String)

'Deletes the file for overwriting
If File.Exists(path) Then
File.Delete(path)
End If




'Writes variables
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Var1Alias = " + Form1.Var1.ToString)
sw.WriteLine("Var2Alias = " + Form1.Var2.ToString)
...

sw.Close()
End Using
End Sub

and for loading I have


Public Shared Sub Load(path As String)
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader(path)
Dim line As String
' Read the lines from the file until the end of the file is reached and set the appropriate values.
Do
line = sr.ReadLine()
If line <> Nothing Then
DataParse(line)
End If


Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
MessageBox.Show("The file could not be read:" & E.Message & " at " & E.TargetSite.ToString)

End Try
Form1.UpdateLabelText()

End Sub

and data parse is

Public Shared Sub DataParse(line As String)
If line.Contains("Var1Alias = ") Then
line.Replace("Var1Alias = ", "")
Form1.Var1 = Val(line)

ElseIf line.Contains("Var2Alias = ") Then
line.Replace("Var2Alias = ", "")
Form1.Var2 = Val(line)

...


End If


End Sub

but when I try to call my loading subroutine all the variables become zero instead of what the save file dictates that they should be. Does anyone know why this is happening?

Continue reading...
 
Back
Top