Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple File I/O in Visual Basic 2013
    primarykey
    data
    text
    <p>I want to learn how to write and read data from files using Visual Basic Studio Express 2013 for Windows Desktop.</p> <p>Specifically, I have a class called Pilots. The class includes text of a first and last name, and integers representing rank, skill, missions and status.</p> <p>There is an ArrayList called Squadron that contains 18 (at start) Pilots. </p> <p>What I want to do is save and then load all the data in Squadron to a file. And I'm not sure how to get it to work properly. I've read numerous books and sites, but somewhere along the line, I'm just not comprehending how to get it to work correctly.</p> <p>At this point, I've been able to write the data to a file.... but I'm having no luck getting it back out of the file.</p> <p>To write it out... I'm using this code:</p> <pre><code> Dim currentpilot As New Pilot("John", "Doe") ' Create the BinaryWriter and use File.Open to create the file. Using writer As BinaryWriter = New BinaryWriter(File.Open("squadron.dat", FileMode.Create)) ' Write each integer. writer.Write(SquadronList.Count) For index = 0 To SquadronList.Count - 1 currentpilot = CType(SquadronList(index), Pilot) writer.Write(currentpilot.pFirstName) writer.Write(currentpilot.pLastName) writer.Write(currentpilot.pSkill) writer.Write(currentpilot.pRank) writer.Write(currentpilot.pStatus) writer.Write(currentpilot.pMissions) writer.Write(currentpilot.pKills) Next End Using </code></pre> <p>The <code>writer.Write(SquadronList.Count)</code> line is there to record the number of actual Pilot records in the file. I don't know if that's necessary or not.</p> <p>The file does write to disk and just looking at it with notepad, it does seem to have the correct data.</p> <p>The problem is getting it back out. This code fails quickly...</p> <pre><code> Using reader As BinaryReader = New BinaryReader(File.OpenRead("squadron.dat")) Dim index As Integer = reader.ReadInt32 For counter = 0 To index - 1 currentpilot.pFirstName = reader.ReadString currentpilot.pLastName = reader.ReadString currentpilot.pSkill = reader.ReadInt16 currentpilot.pRank = reader.ReadInt16 currentpilot.pStatus = reader.ReadInt16 currentpilot.pMissions = reader.ReadInt16 currentpilot.pKills = reader.ReadInt16 SquadronList.Add(currentpilot) Next End Using </code></pre> <p>So... any suggestions or guidance will be appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload