School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,418 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,561 people online right now. Registration is fast and FREE... Join Now!




Saving & Loading

 
Reply to this topicStart new topic

> Saving & Loading, 1d Array, 2d Array & Lists

AdamSpeight2008
Group Icon



post 26 Mar, 2009 - 01:07 PM
Post #1


Saving an 1D Array
CODE

Private Sub SaveTheArray(ByRef TheArray() As String, ByRef Loci As String)
  If My.Computer.FileSystem.FileExists(Loci) Then
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
    MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
   End If
   My.Computer.FileSystem.DeleteFile(Loci)
  End If
  Try
   Dim fs As New IO.StreamWriter(Loci, IO.FileMode.Truncate)
   For Each ae As String In TheArray
    fs.WriteLine(ae)
   Next
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub


Loading a 1-D Array
CODE

Private Sub LoadTheArray(ByRef TheArray() As String, ByRef loci As String)
  If My.Computer.FileSystem.FileExists(loci) = False Then
   MessageBox.Show("File not found")
   Exit Sub
  End If
  Try
   Dim fs As New IO.StreamReader(loci)
   Dim i As Integer = 0
   While fs.EndOfStream = False
    ReDim Preserve TheArray(i)
    TheArray(i) = fs.ReadLine
    i += 1
   End While
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub



Saving the 2D Array out of a file.
CODE

Private Sub Save_2D_Array(ByRef b(,) As Integer, ByRef loci As String)
  ' Does the file already exist?
  If My.Computer.FileSystem.FileExists(loci) Then
   ' Yes, the file exists.
   ' Ask the user to decide wether to continue.
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
    ' The user doesn't want to contine. Display message, then exit.
    MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
   End If
   ' Delete the existing file, other it appends (Why?)
   My.Computer.FileSystem.DeleteFile(loci)
  End If
  ' Using a Try - Catch Statement, just incase their is a problem with the file.
  Try
   ' Create a Stream into which we can write.
   Dim fs As New IO.StreamWriter(loci, IO.FileMode.Create)
   ' Temporary String to which we concatenate columns of each row.
   Dim line As String = String.Empty
   ' For-Loop each row
   For g_row As Integer = LBound(b, 1) To UBound(b, 1)
    ' Remembering to Empty The String
    line = String.Empty
    ' For-Loop each column
    For g_col As Integer = LBound(b, 2) To UBound(b, 2)
     ' Is this the before the last column?
     If g_col < UBound(b, 2) Then
      ' Yes, so concatenate the contents at this row and column on the end of the temporary string.
      ' Add a comma as well
      line &= g(g_row, g_col) & ","
     Else
      ' Nope, Last Column so concatenate the contents without a comma.
      line &= g(g_row, g_col)
     End If
    Next
    ' Write the string into the stream.
    ' ** INFO ** The string isn't necassarly written into the file straight away.
    fs.WriteLine(line)
   Next
   ' Close the stream, at which point the stream is written into the file.
   fs.Close()
   ' Just in case the stream throws an exception catch.
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub


Loading the 2D Array out of a file.
CODE

Private Sub Load_2D_Array(ByRef b(,) As Integer, ByRef loci As String)
  Try
   Dim fs As New IO.StreamReader(loci)
   Dim line As String = ""
   Dim g_row As Integer = 0
   While fs.EndOfStream = False
    line = fs.ReadLine
    Dim c() As String = line.Split(",")
    Dim i As Integer = 0
    For Each ce As String In c
     b(g_row, i) = CInt(ce)
     i += 1
    Next
    g_row += 1
   End While
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub



Saving a List
CODE

Private Sub SaveTheList(ByRef TheList As List(Of String), ByRef Loci As String)
  If My.Computer.FileSystem.FileExists(Loci) Then
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
    MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
   End If
  End If
  Try
   Dim fs As New IO.StreamWriter(Loci, IO.FileMode.OpenOrCreate)
   For Each ae As String In TheList
    fs.WriteLine(ae)
   Next
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub


Loading a List
CODE

Private Sub LoadTheList(ByRef TheList As List(Of String), ByRef loci As String)
  If My.Computer.FileSystem.FileExists(loci) = False Then
   MessageBox.Show("File not found")
   Exit Sub
  End If
  TheList.Clear()
  Try
   Dim fs As New IO.StreamReader(loci)
   While fs.EndOfStream = False
    TheList.Add(fs.ReadLine)
   End While
  Catch ex As System.IO.IOException
   Throw ex
  End Try
End Sub


Notes to remember
  • Writing to a stream doesn't mean it written straight into the file.
  • Load contents in the same order in which they were saved.
  • Treat the read as if it was is a string, cast to the correct type.


I personally prefer the method used the linked tutorial, as it save the missing about of casting string to different types.
Saving Objects Tutorial
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 11:36PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month