Technology Microsoft Software & solutions

All About Serializing in Visual Basic

< Continued from page 2

The example we just coded only serialized the data, but suppose you need to control how the data is serialized. VB.NET can do that too!

To accomplish this, you need to get a little deeper into the concept of serialization. VB.NET has a new object to help out here: SerializationInfo. Although you have the ability to code custom serialization behavior, it comes with a cost of extra coding.

The basic extra code is shown below.


Remember, this class is used instead of the ParmExample class shown in the earlier example. This isn't a complete example. The purpose is to show you the new code that is needed for custom serialization.

Imports System.Runtime.Serialization
<Serializable()> _
Public Class CustomSerialization
   Implements ISerializable
   ' data to be serialized here
   ' Public SerializedVariable as Type
   Public Sub New()
   ' default constructor when the class
   ' is created - custom code can be
   ' added here too
   End Sub
   Public Sub New( _
      ByVal info As SerializationInfo, _
      ByVal context As StreamingContext)
      ' initialize your program variables from
      ' a serialized data store
   End Sub
   Public Sub GetObjectData( _
      ByVal info As SerializationInfo, _
      ByVal context As StreamingContext) _
      Implements ISerializable.GetObjectData
      ' update the serialized data store
      ' from program variables
   End Sub
End Class


The idea is that now you can (and, in fact, you must) do all of the updating and reading of data in the serialized data store in the New and GetObjectData subroutines.

You must also include a generic New constructor (no parameter list) because you're implementing an interface.

The class will normally have formal properties and methods coded as well ...

' Generic Property
Private newPropertyValue As String
Public Property NewProperty() As String
   Get
      Return newPropertyValue
   End Get
   Set(ByVal value As String)
      newPropertyValue = value
   End Set
End Property

' Generic Method
Public Sub MyMethod()
   'method code
End Sub


The resulting serialized class can create unique values in the file based on the code you supply. For example, a real-estate class might update a the value and address of a house but the class would serialize a calculated market classification as well.

The New subroutine will look something like this:

Public Sub New( _
   ByVal info As SerializationInfo, _
   ByVal context As StreamingContext)
   ' initialize your program variables from
   ' a serialized data store
   Parm1Name = info.GetString("a")
   Parm1Value = info.GetInt32("b")
   ' New sub continues ...


When Deserialize is called on a BinaryFormatter object, this sub is executed and a SerializationInfo object is passed to the New subroutine. New can then do whatever is necessary with the serialized data values. For example ...

MsgBox("This is Parm1Value Times Pi: " _
   & (Parm1Value * Math.PI).ToString)


The reverse happens when Serialize is called, but the BinaryFormatter object calls GetObjectData instead.

Public Sub GetObjectData( _
   ByVal info As SerializationInfo, _
   ByVal context As StreamingContext) _
   Implements ISerializable.GetObjectData
   ' update the serialized data store
   ' from program variables
   If Parm2Name = "Test" Then
      info.AddValue("a", "This is a test.")
   Else
      info.AddValue("a", "No test this time.")
   End If
   info.AddValue("b", 2)


Notice that the data is added to the serialized file as name/value pairs.

A lot of the web pages I've found in writing this article don't seem to have actual working code. One wonders whether the author actually executed any code before writing the article sometimes. All of the code use here can be downloaded at this link!

Related posts "Technology : Microsoft Software & solutions"

How to Import OCX

Microsoft

How to Install Grub From a Fedora Recovery CD

Microsoft

How to Speed Up Streaming in Vista

Microsoft

How to Fix a Disappearing "Start" Menu, "Start" Button and Task Bar

Microsoft

How Do I Modify Startup in Vista?

Microsoft

How to Change Your Name in CP

Microsoft

How to Use WinImage to Create a Boot Disk ISO

Microsoft

How to Get to a BIOS Screen

Microsoft

How to Control Updates on XP

Microsoft

Leave a Comment