Skip to content

VB.NET – Object Serialization

October 8, 2005

Object Serialization is a new concept introduced in VB.NET. This concept already exists in high level languages like JAVA but it is introduced in VB.NET for the first time.

Some of you who are new to object serialization must be asking a question ‘What is Object Serialization?’. Well, the answer to this question is very important for you. Object Serialization is a technique in which you can save a class to a file just like objects that are of type integer and strings. You make a class ‘Serializable’ and then you get the capability to save that class to a file, say a text file. Then, it becomes easy for you to load the objects from the file and read them later. In other words, Serialization converts a class into a format in which it can be saved to a file.

Explaining and understanding this technique may not give you that much idea whereas practical example will give an exact idea of what we are talking about. So let’s examine this technique with a sample example below:

For serialization, you need to have a class first. So first, start a new project in VB.NET. Then make a new class. Follow the steps below to create a new class:

  1. Right-Click at the current project’s name (‘WindowsApplication1′ by default) in the Solution Explorer.
  2. Select ‘Add’ >> ‘Add Class’.
  3. Select ‘Class’ from the templates collection and rename the class to ‘Person.vb’.
  4. Press ‘Open’

Now, write the following code in the class code window:

<Serializable()> _
Public Class Person

Private name As String
Private age As Integer

Public Function SetName(ByVal n As String)

name = n

End Function

Public Function GetName() As String

Return name

End Function

Public Function SetAge(ByVal a As Integer)

age = a

End Function

Public Function GetAge() As Integer

Return age

End Function

End Class

This is a simple example of writing a class. This class holds the name and age of a person. Note the statement ” at the beginning. This statement makes class a serializable object which can then be written to a file. The rest of the code of this class needs no explanation as it has simple ‘Set’ and ‘Get’ functions to set and get the name and age of a person, which are the data types of this class.

Now, class is created and we are ready to make it’s instances and write them to a file. Please note that a class is serialized and de-serialized with the help of two objects: ‘BinaryFormatter’ and the ‘Stream’ object. For using these objects, you need to import their packages by adding these lines at the beginning of a form module as follows:

Imports System.IO 'For Stream object

Imports System.Runtime.Serialization.Formatters.Binary 'For BinaryFormatter object

Saving a serialized class

For saving a serialized class, you first have to make it’s instance and then set it’s values as follows:

Dim p As New Person()

The class is initialized now and it has it’s data values set now. Write the following code to save it to a file:

Dim b As New BinaryFormatter()
Dim f As Stream
f = File.Open("D:/data.txt", FileMode.CreateNew)
b.Serialize(f, p)

You can see the use of ‘BinaryFormatter’ and ‘Stream’ objects to serialize and save a class object to file. A ‘Stream’ is initialized by using the ‘File’ object’s ‘Open’ function which takes two parameters: First one is the name of the file in which you want to save the object and the second one is the mode in which you want to open the file. You can choose the modes from FileMode.Append, FileMode.Create, FileMode.CreateNew, FileMode.Open, FileMode.OpenOrCreate or FileMode.Truncate depending upon the operation you want to perform on the file. Then we call the ‘BinaryFormatter’ object’s ‘Serialize’ function which again takes two parameters: First one is the stream through which you are saving the file and the second one is the object which you want to serialize and save. This function serializes the object and saves it the file. Finally, the stream is closed.

Reading a serialized class

Reading a serialized class object saved in a file is a process similar to saving it in a file. Have a look at the code below:

Dim p As Cls
Dim b As New BinaryFormatter()
Dim f As Stream
f = File.Open("D:/dd.txt", FileMode.Open)
p = b.Deserialize(f)

MsgBox("Name: " & p.GetName & vbCrLf & "Age: " & p.GetAge)

Here, we initialize a ‘Stream’ object by calling the ‘File’ object’s ‘Open’ function to open the file. This function takes ‘FileMode.Open’ as the second parameter this time as we are opening a file for reading. Then we call the ‘BinaryFormatter’ object’s ‘Deserialize’ function which takes an open stream as parameter. This function reads a file through the stream, deserializes the object contained in it and then loads and initializes it in the memory. Finally, the stream is closed and you can call the object’s ‘Get’ functions to retrieve the values that you saved earlier.


Tell us what do you think.

There are no comments on this entry.


Websites mentioned my entry.

There are no trackbacks on this entry

Add a Comment

Fill in the form and submit.