1. Computing

Visual Basic .NET 2010 Express - Using Data and Serializing From Files

Part 7 of an About Visual Basic Tutorial

From , former About.com Guide

Updated May 23, 2010

This is a free tutorial to help beginning programmers get up to speed using Microsoft Visual Basic 2010 Express. To get the most from this tutorial, you might want to start at the beginning. A complete index to the course is also in the front of the first lesson.

Part 1 - A "From the Ground Up" Tutorial - An introduction to the course.

In this lesson and in the next one, we'll be developing two new versions of the About Visual Basic Signature Block program. Each will emphasize a different technology to manage data. In this lesson, we'll use just just one of those technolgies: serializing data. In the next lesson, we'll use an XML document instead. Both of these techniques have strong arguments to recommend them and we'll look at the differences between them to get more insight into the use of data in programming.

And while we're developing this improved version, we'll take a look at several more detailed parts of VB.NET that we'll need in the example applications here:

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

Data technologies are moving fast!

You may not even know what "serializing" means. (But we'll cover that soon.) Although this is a course for beginners, the technologies that we will use are pretty advanced. Here's my philosophy on that:

Many courses start you out with simple, but seriously outdated techniques. For example, to read and write to files, some courses still use "comma delimited files" also called "comma separated values" (file.csv files) using the Visual Basic Input and Write statements. These techniques were the mainstream way to deal with small amounts of data in VB6 and earlier, but .NET has much better technology. It seems to me that it just doesn't make sense to teach techniques that are already obsolete just to make examples a litte easier. So this course teaches the latest technology, but we proceed slowly and carefully.

In the previous lesson, 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 solve some of those design flaws by using serialization to keep the information in a file instead of just trying to keep it internally in program memory. The program won't save the information in the file quite yet. That will be added in a future version.

Coding an Improved SigBlock Program

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

Open the program from Part 6 and add a new Class item to your project. (Remember to name it SigBlockStruct before you save it! It's easier than renaming it later.) This gives you a little more than you need because VB Express adds the Class and End Class statements 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 this is just a convenient way to add the file that we need.

Delete everything inside the new Class file. Open the SigBlock.vb module in a different tab and move the structure from the existing SigBlock.vb code into the empty structure file. You'll note that VB.NET Express flags the Structure statement as an error but it helps you out with a hint to change the type to Public since it will now be referenced from a different code module.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

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 special code when the VB code is compiled that will be used when the structure is actually serialized later.

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 entire object around more easily. For example, the entire structure could be a parameter passed from one program to another. We're 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 article about serialization. (Read the serialization article by clicking here.)

We're also 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

The completed structure, now in a file by itself, will look like the illustration below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2010 Express - Using Data and Serializing From Files

©2013 About.com. All rights reserved.