Serialization is the process of converting an object into a linear sequence of bytes called a "byte stream." Deserialization just reverses the process. But why would you want to convert an object into a byte stream?
The main reason is so you can move the object around. Consider the possibilities. Since "everything is an object" in .NET, you can serialize anything and save it to a file. So you could serialize pictures, data files, the current state of a program module ('state' is like a snapshot of your program at a point in time so you could temporarily suspend execution and start again later) ... whatever you need to do.
You can also store these objects on disk in files, send them over the web, pass them to a different program, keep a backup copy for safety or security. The possibilities are quite literally endless.
That's why serialization is such a key process in .NET and Visual Basic. I've written about it before, but in this article, I've added a section on custom serialization by implementing the ISerializable interface and coding a New and a GetObjectData subroutine.
As a first example of serialization, let's do one of the easiest programs, but also one of the most useful: serializing data, and then deserializing data in simple class to and from a file. In this example, the data is not only serialized, but the structure of the data is saved too. The structure here is declared in a module to keep things ... well ... structured.
Module SerializeParms
<Serializable()> Public Class ParmExample
Public Parm1Name As String = "Parm1 Name"
Public Parm1Value As Integer = 12345
Public Parm2Name As String
Public Parm2Value As Decimal
End Class
End Module
Then, individual values can be saved to a file like this:
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public Class Form1
Private Sub mySerialize_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mySerialize.Click
Dim ParmData As New ParmExample
ParmData.Parm2Name = "Parm2 Name"
ParmData.Parm2Value = 54321.12345
Dim s As New FileStream("ParmInfo", FileMode.Create)
Dim f As New BinaryFormatter
f.Serialize(s, ParmData)
s.Close()
End Sub
End Class
And those same values can be retrieved like this:
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public Class Form1
Private Sub myDeserialize_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles myDeserialize.Click
Dim s = New FileStream("ParmInfo", FileMode.Open)
Dim f As New BinaryFormatter
Dim RestoredParms As New ParmExample
RestoredParms = f.Deserialize(s)
s.Close()
Console.WriteLine(RestoredParms.Parm1Name)
Console.WriteLine(RestoredParms.Parm1Value)
Console.WriteLine(RestoredParms.Parm2Name)
Console.WriteLine(RestoredParms.Parm2Value)
End Sub
End Class
A Structure or a collection (such as an ArrayList) rather than a Class could also be serialized to a file this same way.
Now that we have gone over the basic serializing process, lets look at the specific details that are part of the process on the next page.
