The last thing we need to do is tie up the loose ends and demonstrate one way to run the entire program. I can't emphasize enough that these are fundamental programming tools. They can be used in an endless number of different ways. But this one works and it will get you started.
First, Here's the entire program source (for the Soap XML version). An explanation of the new lines follows the code.
Imports System.IO
Imports System
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Soap
<serializable()> Public Class VBLesson
Public LessonTitle As String
Public LessonNumber As Int16
End Class
Module Module1
Sub Main()
SaveSerializedFile()
RestoreSerializedFile()
End Sub
Sub SaveSerializedFile()
Dim LessonList As New ArrayList
Dim LessonInfo As New VBLesson
LessonInfo.LessonTitle = "Why did they break VB?"
LessonInfo.LessonNumber = 1
LessonList.Add(LessonInfo)
LessonInfo = New VBLesson
LessonInfo.LessonTitle = "VB.NET in Three Acts"
LessonInfo.LessonNumber = 2
LessonList.Add(LessonInfo)
LessonInfo = New VBLesson
LessonInfo.LessonTitle = "You're Not a Beginner Anymore!"
LessonInfo.LessonNumber = 3
LessonList.Add(LessonInfo)
Dim s As New FileStream("lessoninfo.txt", FileMode.Create)
Dim f As New SoapFormatter
f.Serialize(s, LessonList)
s.Close()
End Sub
Sub RestoreSerializedFile()
Dim s = New FileStream("lessoninfo.txt", FileMode.Open)
Dim f As New SoapFormatter
Dim RestoredLessonList As ArrayList
RestoredLessonList = CType(f.Deserialize(s), ArrayList)
s.close()
Dim x As VBLesson
For Each x In RestoredLessonList
Console.WriteLine(x.LessonTitle & _
" Lesson Number " & x.LessonNumber)
Next
Console.WriteLine("Press Enter to Continue ...")
Console.ReadLine()
End Sub
End Module
To use our first 'cheap' method - the free VBC compiler in the .NET SDK - one more thing has to be done. You have to tell the VBC compiler where the critical objects can be found on your computer. These objects are usually in what are called DLL files (Dynamic Link Library) and the use of DLL files has been a part of Visual Basic for almost a decade now. If you're using Visual Studio, add them in the "References" window as shown at the left side of the screen. (It's easiest to right click the Project and select Add Reference ...)
To do the same thing with the VBC command line compiler, you have to use a parameter to tell the compiler that you need the SoapFormatter object (the others are in a default library that VBC already knows how to find). This is the command that is entered into a DOS window to compile the entire program if you didn't change the default name: module1.vb:
vbc module1.vb /r:System.Runtime.Serialization.Formatters.Soap.dll
In this program, I have also chosen to use Imports statements to make the lines of code shorter in the program.
Imports System
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Soap
The Imports statement only lets the compiler complete object references in your program automatically. In other words, these two ways of declaring the SoapFormatter object are equivalent:
Imports System.Runtime.Serialization.Formatters.Soap
...
Dim f As New SoapFormatter
or
Dim f As New _
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
The only other "new code" in the program is a Main subroutine that calls the other two subroutines that do the heavy lifting. If no other entry point is specified, VB.NET looks for a Main subroutine to start executing.
Once the program has been successfully compiled (no errors), you run the program from the command like by simply entering the name.
Although a lot of this lesson has focused on serialization, keep in mind that .NET and other object libraries provide an endless supply of functions for you to use in your code. The real subject of this lesson is a practical example of just one of them that, hopefully, will help you when you use others.
Return to the first page >
The Lesson Goals > Page
1,
2,
3,
4