title: How would I write formatted data to a file and then read it? VB.NET (2012 edition)
body: <p>I'm practicing VB.NET and I've got a problem with Reading and writing to a .dat file. I have made a structure to store data temporarily (below).</p>
  
  <pre><code>Structure CustomerType
      Dim AccountNum As String
      Dim Surname As String
      Dim Forename As String
      Dim Balance As Decimal
  End Structure
  </code></pre>
  
  <p>I then Dim everything.</p>
  
  <pre><code>Dim Customers(9) As CustomerType
  Dim Filename As String = "Accounts.dat"
  Dim NumberOfRecords As Short = 0
  Dim myFormat As String = "{0,-15}|{1,-15}|{2,-10}|{3,-10}"
  </code></pre>
  
  <p>I have a button that creates a new account and this is where I get the problem.</p>
  
  <pre><code>    FileOpen(1, Filename, OpenMode.Random, , , )
      For i = 1 To Customers.Length() - 1
          With Customers(i)
              .Forename = InputBox("First name", "Forename")
              Do Until .Forename &lt;&gt; "" And TypeOf .Forename Is String
                  .Forename = InputBox("First name", "Forename")
              Loop
              .Surname = InputBox("Surname", "Surname")
              Do Until .Surname &lt;&gt; "" And TypeOf .Surname Is String
                  .Surname = InputBox("Surname", "Surname")
              Loop
              .AccountNum = InputBox("Account Number of " &amp; Customers(i).Forename &amp; " " &amp; Customers(i).Surname &amp; ".", "Account Number")
              Do Until .AccountNum.Length = 8 And TypeOf .AccountNum Is String
                  .AccountNum = InputBox("Account Number of " &amp; Customers(i).Forename &amp; " " &amp; Customers(i).Surname &amp; ".", "Account Number")
              Loop
              .Balance = InputBox("Balance of " &amp; Customers(i).Forename &amp; " " &amp; Customers(i).Surname &amp; ".", "Balance")
              Do Until .Balance &gt; -1
                  .Balance = InputBox("Balance of " &amp; Customers(i).Forename &amp; " " &amp; Customers(i).Surname &amp; ".", "Balance")
              Loop
              FilePut(1, Customers, NumberOfRecords + 1)
              NumberOfRecords += 1
              lblNumberOfRecords.Text = NumberOfRecords
          End With
      Next
      FileClose(1)
  </code></pre>
  
  <p>I have another button that displays the data in a listbox. I can only get one item to display before I get a bad length error.</p>
  
  <pre><code>    Dim Index As Integer
      ListBox1.Items.Clear()
      ListBox1.Items.Add(String.Format(myFormat, "Forename", "Surname", "Acc. Num.", "Balance"))
      ListBox1.Items.Add("_____________________________________________________")
      FileOpen(1, Filename, OpenMode.Random, , , )
      For Index = 1 To NumberOfRecords
          FileGet(1, Customers)
          ListBox1.Items.Add(String.Format(myFormat, Customers(Index).Forename, Customers(Index).Surname, Customers(Index).AccountNum, Format(Customers(Index).Balance, "currency")))
      Next Index
      FileClose(1)
  </code></pre>
  
  <p>The main question that I have is What am I doing wrong, and how can I fix it?</p>
  
  <p>Many Thanks in advance,
  Jordan</p>