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

Visual Basic .NET 2008 Express - Using Data and Serializing to Files
The Const Statement

By Dan Mabbutt, About.com

Jun 7 2008

Since we're going to save our data to a file, we code a Const to describe where the file is. Again, this just makes things a little easier. Make this the first statement in the SigBlock class ...

Private Const SigBlockFile As String = "SigBlockData.txt"

The first method that hasn't been coded is one to show the window that will allow the display of the different fields in the signature block to be turned on and off. We pass the whole structure as an object to this method:

Public Sub SigFieldsShow(ByRef mySigBlockStruct As SigBlockStruct)

End Sub

Add this code to the end of the SigBlock class. There are three kinds of statements we'll add to this method, but first, there's something new in the statement we just added.

ByVal and ByRef

There's a new twist here. All the methods you have seen so far use a parameter list like this:

SubName(ByVal Name as Type)

This one uses ByRef instead. What's the difference?

Arguments are passed either by making a copy of the argument and using that (ByVal - just the value is passed) or by passing the actual argument (ByRef - a reference to where the argument is in memory is passed). The difference is whether the value of the argument can be changed after the return from the method. In most cases, you don't want the argument changed because the method will just use the information. In this case, we do want the values in the structure to be changed. That's why the structure is being passed - to change the values in the variables.

Modal Forms

The first series of statements in the SigFieldsShow method set the properties of the CheckBox components in the form that will be displayed with a series of statements like this:

If mySigBlockStruct.FNameChecked Then _
   SigFieldDisplay.FName.Checked = True

Code this statement for each field in the structure.

The second type of statement is just the single line to display the form:

SigFieldDisplay.ShowDialog()

But there's another twist. The ShowDialog() method makes the form modal. This means, first, that once the form appears, it has to be closed again before any other part of the application can be accessed. And second, the code following it is not executed until after it's closed. But after the modal form is closed, we can update our structure with any changed CheckBox values with the third series of statements which all look like this:

mySigBlockStruct.FNameChecked = _
   SigFieldDisplay.FName.Checked

Code a statement like this for each field in the structure.

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 and an intrinsic constant 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 create the data in our entire structure from a serialized file is declared and then the method to do that magic is simply called.

The GetLines method has to be changed to get the information from the structure instead of the CheckBox component property values. That method starts like this:

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

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, we can use them directly in the If statement.

Delete the unused private member variable from the previous program (m_SigBlock) and we're good to go. 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.

So ... 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, Conditional Statements and 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.

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 2008 Express - Using Data and Serializing to Files

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

All rights reserved.