1. Computing

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

Reading the Serialized Signature File


The final method that hasn't been coded is the Load method. Here's the code for that:

Public Sub Load(ByRef mySigBlockStruct As SigBlockStruct)
    Dim FS As New System.IO.FileStream(SigBlockFile, FileMode.Open)
    Dim BinFormatter As New Binary.BinaryFormatter
    mySigBlockStruct = BinFormatter.Deserialize(FS)
End Sub

The bottom line here is that .NET just has some really powerful capability and all you have to do is know how to call on it. This method declares and opens a new "streaming" file called FS using the constant we declared earlier and an "intrinsic constant", FileMode.Open, provided by VB Express. (Intrinsic constants were explained when vbCrLf, another intrinsic constant, was used in the first version of our program.) A new object that will hold the data in our entire structure from a serialized file is declared and then the method to do that magic is simply called.

The previous version used a "Lines()" subroutine to build a long string that would be displayed. This is replaced by a GetLines method that gets the information from the structure. That method starts like this:

    Public Function GetLines(
        ByVal mySigBlockStruct As SigBlockStruct
        ) As String
        GetLines = System.String.Empty
        If mySigBlockStruct.FNameChecked Then _
            GetLines &= mySigBlockStruct.FName & " "

The GetLines method is coded as a Function. That means that when it's called, a value is assigned to the Function just as thought it was a variable. In this case, values are assigned to GetLines. Add a statement for each checkbox to complete the Function.

Note the initialization of GetLines: In VB6, we would have initialized the string to an empty string:

GetLines = ""

In .NET, it's better to use the system object provided for that purpose (even though using an empty string would still work).

Since the "Checked" variables in the structure are Boolean values (True or False), we can use them directly in the If statement.

Add the End Function statement to finish off the function and we're good to go. The final result is shown in the illustration below.

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

But even if you've done everything right, the program still won't quite run. Why? Well ... the program assumes that starting values for the structure are already in the serialized file, SigBlockData.txt and nothing has been done to create that file. We have a sort of "chicken and egg" problem. To make the program actually run, it's necessary to code a quick utility program that does nothing more than create the file. That program has also been included with the downloadable source code and it looks a lot like the Load method that we have already seen so I won't explain it in detail.

This is a much improved version of the system. To get the complete source code, download it here.

Part 8 of the tutorial, Using XML, explores some of the fundamental ways that programs are organized to process data. And a more advanced version of the About Visual Basic Signature Block program is coded where we use an XML file to store the data used by the program.

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

©2014 About.com. All rights reserved.