So i'm a total noob in visual basic.net, and I need to write some data to a binary file so it can be retrived in the next execution.
The variable that is writing is a class(Targets) containing an array of another class named t, the sub-class(target) contains the real data in 22 variables, also the class targets has an integer counter of how many instances are in the array(nfarms), hope it's clear, i can re-explain if needed.
The problem is that when the program tries to read the file gets stuck at the first line of the while, at Targets.t(x).x = objBR.ReadString() from the reading function, but the program do not crashes, just stops, maybe i made a mistake on the writing or the reading functions, i dont know.
The file generates correctly and the program is able to retrive the nfarms value correctly(the first one written), bun when it enters into the loop, fails.
Thanks in advance!
This is the writing function:
And the reading function:
The variable that is writing is a class(Targets) containing an array of another class named t, the sub-class(target) contains the real data in 22 variables, also the class targets has an integer counter of how many instances are in the array(nfarms), hope it's clear, i can re-explain if needed.
The problem is that when the program tries to read the file gets stuck at the first line of the while, at Targets.t(x).x = objBR.ReadString() from the reading function, but the program do not crashes, just stops, maybe i made a mistake on the writing or the reading functions, i dont know.
The file generates correctly and the program is able to retrive the nfarms value correctly(the first one written), bun when it enters into the loop, fails.
Thanks in advance!
This is the writing function:
VB.NET:
Public Sub save_target_file()
Dim objBW As BinaryWriter
Dim objFS As FileStream
Dim x As Integer
x = 1
Testingbox.AppendText(Environment.NewLine + "Nfarms " + Targets.nfarms.ToString)
If (Targets.nfarms > 0) Then
objFS = New FileStream("Targets.bin", FileMode.OpenOrCreate, FileAccess.Write)
objBW = New BinaryWriter(objFS)
objBW.BaseStream.Seek(0, SeekOrigin.Begin)
objBW.Write(Targets.nfarms)
While (x <= Targets.nfarms)
Testingbox.AppendText(Environment.NewLine + "Writing farm number " + x.ToString)
objBW.Write(Targets.t(x).x)
objBW.Write(Targets.t(x).y)
objBW.Write(Targets.t(x).name)
objBW.Write(Targets.t(x).race)
objBW.Write(Targets.t(x).priority)
objBW.Write(Targets.t(x).distance)
objBW.Write(Targets.t(x).raid_ratio)
objBW.Write(Targets.t(x).num_atacks)
objBW.Write(Targets.t(x).total_profit)
objBW.Write(Targets.t(x).auto_balance)
objBW.Write(Targets.t(x).reset_raid_time)
objBW.Write(Targets.t(x).never_raided)
objBW.Write(Targets.t(x).min_atack_capacity)
objBW.Write(Targets.t(x).min_raid_time_interval)
objBW.Write(Targets.t(x).max_raid_time_interval)
objBW.Write(Targets.t(x).time_remaining)
objBW.Write(Targets.t(x).population)
objBW.Write(Targets.t(x).wall)
objBW.Write(Targets.t(x).palace)
objBW.Write(Targets.t(x).traps)
objBW.Write(Targets.t(x).Activated)
objBW.Write(Targets.t(x).inactive)
Testingbox.AppendText(Environment.NewLine + "finalized Writing farm number " + x.ToString)
x = x + 1
End While
objBW.Close()
objFS.Close()
End If
End Sub
And the reading function:
VB.NET:
Public Sub load_target_file()
Dim x As Integer
Dim objBR As BinaryReader
Dim objFS As FileStream
x = 1
If File.Exists("Targets.bin") Then
'open the file
Testingbox.AppendText(Environment.NewLine + "File exists")
objFS = New FileStream("Targets.bin", FileMode.Open, FileAccess.Read)
objBR = New BinaryReader(objFS)
objBR.BaseStream.Seek(0, SeekOrigin.Begin)
Targets.nfarms = objBR.ReadInt32() 'read the number of farms
Testingbox.AppendText(Environment.NewLine + "Farms detected: " + Targets.nfarms.ToString)
While (x <= Targets.nfarms)
Testingbox.AppendText(Environment.NewLine + "Farm " + x.ToString + "is being read")
Targets.t(x).x = objBR.ReadString()
Targets.t(x).y = objBR.ReadString()
Targets.t(x).name = objBR.ReadString()
Targets.t(x).race = objBR.ReadString()
Targets.t(x).priority = objBR.ReadInt32()
Targets.t(x).distance = objBR.ReadInt32()
Targets.t(x).raid_ratio = objBR.ReadInt32()
Targets.t(x).num_atacks = objBR.ReadInt32()
Targets.t(x).total_profit = objBR.ReadInt32()
Targets.t(x).auto_balance = objBR.ReadBoolean()
Targets.t(x).reset_raid_time = objBR.ReadBoolean()
Targets.t(x).never_raided = objBR.ReadBoolean()
Targets.t(x).min_atack_capacity = objBR.ReadInt32()
Targets.t(x).min_raid_time_interval = objBR.ReadInt32()
Targets.t(x).max_raid_time_interval = objBR.ReadInt32()
Targets.t(x).time_remaining = objBR.ReadInt32()
Targets.t(x).population = objBR.ReadInt32()
Targets.t(x).wall = objBR.ReadInt32()
Targets.t(x).palace = objBR.ReadInt32()
Targets.t(x).traps = objBR.ReadBoolean()
Targets.t(x).Activated = objBR.ReadBoolean()
Targets.t(x).inactive = objBR.ReadBoolean()
Testingbox.AppendText(Environment.NewLine + "Farm " + x.ToString + "Readed")
x = x + 1
End While
objBR.Close()
objFS.Close()
Testingbox.AppendText(Environment.NewLine + "File closed")
End If
End Sub