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

JumpyNET

Well-known member
Joined
Apr 4, 2005
Messages
151
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.
[VB]
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

[/VB]


[VB]
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
[/VB]


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
 
Arrays that contain arrays pose certain problems (structures can also have issues...) so you arent 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.
 
Im using VB 2005 Express. Oh and I dont mind changing the arrays to "List(of"s. Can you give me some more instructions?
 
Last edited by a moderator:
Quick version using List(of ) instead of arrays
Code:
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

<Serializable()> _
Public Class OwnStructB

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

End Class

<Serializable()> _
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.
 
Back
Top