Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How can I save an array of custom structure type to a single file on harddisk?

I gues I would need to get the starting address and length of "ThisNeedsToBeStored"-array and save it byte by byte, but I dont know how to do it in VB.

 

My objective is to save and read back the array to a file as easily and fast as possible.

 

Below is an example of a structure I am talking about.

   Public Structure OwnStructB

       Dim Aaaa As String
       Dim Bbbb As Date
       Dim Cccc() As String

   End Structure

   Public Structure OwnStructA

       Dim Aaaa As String
       Dim Bbbb As Date
       Dim Cccc() As String
       Dim Dddd() As OwnStructB

   End Structure

   Public ThisNeedsToBeStored() As OwnStructA

 

 

   Public Sub SaveObjectToFile(ByVal TheObject As OwnStructA, ByVal SaveFilePath As String)
       '?????
   End Sub

   Public Sub GetObjectFromFile(ByRef TheObject As OwnStructA, ByVal SaveFilePath As String)
       '?????
   End Sub

 

 

PS: I did find one solution that can save and restore a structure, but I could not modify it to work with an array containing arrays. It uses serialization. Here is the link:

http://www.thescripts.com/forum/thread368181.html

  • Administrators
Posted

Arrays that contain arrays pose certain problems (structures can also have issues...) so you aren't doing yourself any favours here ;)

 

What version of .Net are you using? If you are using .Net 2 or later then replacing the arrays with generic collections (e.g. List(of ...)) would make this a lot simpler.

 

The articles at http://msdn.microsoft.com/msdnmag/issues/02/04/net/default.aspx and http://msdn.microsoft.com/msdnmag/issues/02/07/net/default.aspx are probably worth a read if you are looking at implementing your own custom serialisation mechanism in .Net 1 though.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)
I'm using VB 2005 Express. Oh and I don't mind changing the arrays to "List(of"s. Can you give me some more instructions? Edited by JumpyNET
  • Administrators
Posted

Quick version using List(of ) instead of arrays

Public Class Form1
   Public ThisNeedsToBeStored As New List(Of OwnStructA)

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


       ThisNeedsToBeStored.Add(New OwnStructA)
       ThisNeedsToBeStored(0).Aaaa = "Test"
       ThisNeedsToBeStored(0).Cccc.Add("Testing")
       ThisNeedsToBeStored(0).Dddd.Add(New OwnStructB)
       ThisNeedsToBeStored(0).Dddd(0).Cccc.Add("sdfdsfdsfsdf")

       Dim fs As New System.IO.FileStream("c:\test.txt", IO.FileMode.Create)
       Dim s As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

       s.Serialize(fs, ThisNeedsToBeStored)

       fs.Close()

   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim fs As New System.IO.FileStream("c:\test.txt", IO.FileMode.Open)
       Dim s As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

       Dim x As List(Of OwnStructA)
       x = s.Deserialize(fs)

       fs.Close()
   End Sub
End Class

 _
Public Class OwnStructB

   Public Aaaa As String
   Public Bbbb As Date
   Public Cccc As New List(Of String)

End Class

 _
Public Class OwnStructA

   Public Aaaa As String
   Public Bbbb As Date
   Public Cccc As New List(Of String)
   Public Dddd As New List(Of OwnStructB)

End Class

 

The above is pretty much the quickest thing I could throw together, in practice I would probably tend to make the OwnStructA and OwnStructB more fully implemented classes (Properties, validation etc.) and probably also create a class for the ThisNeedsToBeStored that simply inherits List(Of OwnStructA) as well.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...