1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - Using Data and Serializing to Files
Using Serialization in the Signature Block Program

By Dan Mabbutt, About.com

Jun 7 2008

In an earlier segment, I wrote that the first version of the Signature Block program, "will get the job done, but it has a few serious design flaws." In this version, we use serialization to save the information to a file instead of just trying to keep it internally in program memory.

Start with the previous program for Part 6. Click here if you need to download the Part 6 program again.

Some of the new things that you will see in this program are:

  • The Imports directive
  • The Const statement for declaring a constant value
  • The ShowDialog() method and modal forms
  • Serialization and the <Serializable()> attribute

But most the most important part of this program is the System.IO.FileStream object that we will use to create a file for the signature block data. (Not included as part of ADO.NET according to Microsoft's phylogeny, interestingly enough.)

We're also going to make our signature block data a separate object and pass it to different program objects as a parameter or argument. To do this, we will learn about ByVal versus ByRef in parameters.

To get started, add a new Class item to your project and name it SigBlockStruct before you save it. This gives you a little more than you need when VB Express adds the Class coding automatically. We're going to make this a Structure rather than a Class but VB Express doesn't have a template for a Structure. So delete everything in the new Class. Then cut and paste the structure from the existing SigBlock.vb code into the code window. Change the type to Public since we're going to reference it from other modules.

The <Serializable()> Attribute

Now, add <Serializable()> before the Public keyword. Attributes are just more information that you can provide to VB.NET. For an in depth explanation of attributes, try my four part article about attributes in VB.NET. Read the article here. The attribute in this code tells VB Express to add specialized code so that later on, everything in this structure can be serialized.

But what is serialization? In brief, In brief, serialization is the process of converting an object into a sequence of bytes (called a byte stream) so you can move the object around more easily. We are going to serialize the structure so we can save the whole thing at once in a file.

One of the big improvements in VB.NET was the addition of great support for serialization. I wrote another four part article about serialization. Read the serialization article by clicking here.

Finally, we're going to correct a problem we had last time where we were actually using the properties of the CheckBox components to save part of the information. Since this is information that is part of the signature block, it should be part of the signature block structure. Add a new series of 'Checked' variables for each existing variable like this:

Dim FName As String
Dim FNameChecked As Boolean

Moving to the main form, SigBlockForm, we now have two objects that we're going to use, so add a declaration for the new structure object.

Dim mySigBlockStruct As New SigBlockStruct

The first thing the program will do is initialize our structure with values from a file. So we take advantage of the Load event for the main form to call a new method that we will add to the SigBlock class just a little later to do that. You can double click the main form to get a starting Load event subroutine. The parameters aren't shown again to make the statements fit the page. The mySigBlock.Load method will be coded later.

Private Sub SigBlockForm_Load( ...
   mySigBlock.Load(mySigBlockStruct)
End Sub

Since we're going to depend on our SigBlock object to do more now, replace the line of code that displays the change form with one that asks the SigBlock object to do that. Replace ...

SigFieldDisplay.Show()

... with ...

mySigBlock.SigFieldsShow(mySigBlockStruct)

Again, we haven't coded the SigFieldsShow method, so VB Express will display an error (blue squiggly line) until we do.

Finally, replace mySigBlock.Lines with mySigBlock.GetLines(mySigBlockStruct) in DisplayBlock_Click to retrieve the signature block for display. The statement should look like this:

SigBlockBox.Text = mySigBlock.GetLines(mySigBlockStruct)

The Imports Statement

The SigBlock object, where most of the work is done, is where most of the changes will have to be made. The first change is to add a couple of Imports statements:

Imports System.IO
Imports System.RunTime.Serialization.Formatters

These have to be the first statements in the program. The Imports statement is not required, but it makes things easier. All it does is tell VB Express to try adding the name following Imports to the names of later objects to locate them. This lets you make the fully qualified names of objects in your code a little shorter.

In other words, the full name of an object that we will use is:

System.RunTime.Serialization.Formatters.Binary.BinaryFormatter

Using Imports lets us call it:

Binary.BinaryFormatter

On the next page, we continue the Signature Block program and learn about the Const statement.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2005 Express - Using Data and Serializing to Files

©2009 About.com, a part of The New York Times Company.

All rights reserved.