Results 1 to 4 of 4

Thread: Saving Question...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Saving Question...

    Is there a way to save a complete class to a binary file without having to save each element in the class? Kinda like in VB6, I could place everything in a TYPE and just save the whole TYPE in one big binary chunk.. Is there anything simular to this method of saving data in vb.net?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can serialize an object to disk or memory as long as it has the Serializable attribute. I'll post an example in a sec. You can also serialize things to xml if you want.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. 'the example class
    2. 'make sure the class has the serializable attribute
    3. <Serializable()> _
    4. Public Class Tester
    5.     Private _Name As String = String.Empty
    6.     Private _Other As Integer = 0
    7.  
    8.     Public Property Name() As String
    9.         Get
    10.             Return _Name
    11.         End Get
    12.         Set(ByVal Value As String)
    13.             _Name = Value
    14.         End Set
    15.     End Property
    16.  
    17.     Public Property Other() As Integer
    18.         Get
    19.             Return _Other
    20.         End Get
    21.         Set(ByVal Value As Integer)
    22.             _Other = Value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Sub New()
    27.         'xml serialization has trouble if there is no default constructor
    28.     End Sub
    29.  
    30.     Public Sub New(ByVal name As String, ByVal other As Integer)
    31.         _Name = name
    32.         _Other = other
    33.     End Sub
    34.  
    35. End Class
    36.  
    37.         'the code to test it
    38.         Dim test As New Tester("Test1", 6)
    39.  
    40.         'delete old file if found
    41.         If IO.File.Exists("data.dat") Then IO.File.Delete("data.dat")
    42.         'make the file and stream
    43.         Dim fs As New IO.FileStream("data.dat", IO.FileMode.CreateNew)
    44.         'serialize it to binary data
    45.         Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    46.         bf.Serialize(fs, test)
    47.         fs.Close()
    48.  
    49.         'open file again
    50.         fs = New IO.FileStream("data.dat", IO.FileMode.Open)
    51.         'now deserialize and display data to make sure it worked
    52.         Dim retTest As Tester = bf.Deserialize(fs)
    53.         fs.Close()
    54.         MsgBox(String.Concat("Name:", retTest.Name, " Other:", retTest.Other), , "Binary")
    55.  
    56.         'now here is the samething but to xml
    57.  
    58.         'delete old file if found
    59.         If IO.File.Exists("data.xml") Then IO.File.Delete("data.xml")
    60.         'make the file and stream
    61.         fs = New IO.FileStream("data.xml", IO.FileMode.CreateNew)
    62.         'serialize it to xml data
    63.         Dim xf As New Xml.Serialization.XmlSerializer(GetType(Tester))
    64.         xf.Serialize(fs, retTest)
    65.         fs.Close()
    66.  
    67.         'open file again
    68.         fs = New IO.FileStream("data.xml", IO.FileMode.Open)
    69.         'now deserialize and display data to make sure it worked
    70.         Dim xmlTest As Tester = xf.Deserialize(fs)
    71.         fs.Close()
    72.         MsgBox(String.Concat("Name:", xmlTest.Name, " Other:", xmlTest.Other), , "XML")

    Then afterwards you are left with 2 files data.data with the contents and data.xml with the xml contents.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Very nice, I'll have to put this to use, thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width