Results 1 to 3 of 3

Thread: Serialization

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Serialization

    It is fairly easy to Serialize objects in Visual Basic.
    What is serialization for?

    Code:
    Public Class Data
    
        Public Shared Sub Save(Path As String, Data As Type)
            Dim BF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Using S As New IO.FileStream(Path, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                BF.Serialize(S, Data)
            End Using
        End Sub
    
        Public Shared Function Load(Path As String) As Type
            Dim BF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Using S As New IO.FileStream(Path, IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
                Try
                    Return CType(BF.Deserialize(S), Type)
                Catch ex As Exception
                    ' File not found/cannot be read.
                    Return Nothing
                End Try
            End Using
        End Function
    
    End Class
    Don't forget to add the serializable attribute on the class you wish to serialize.

    Code:
    <Serializable()>
    Public Class SomeType
        ' Properties...
    End Class
    Best of luck,
    NinjaNic

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Serialization

    Small tip. You should encourage programmers to propagate the exception instead of returning Nothing when failing to deserialize from a file. I'm sure there is plenty of debate over which pattern is better, but when dealing with files, it's a common practice to let the user know that the file couldn't be read. Of course you can treat a return value of Nothing as failure but it gives no information on why it failed.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Serialization

    You can read this SO thread if you're interested in the debate concerning returning null vs throwing exceptions.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

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