Click here to Skip to main content
6,632,253 members and growing! (20,309 online)
Email Password   helpLost your password?
Languages » VB.NET » HowTo     Intermediate License: The Microsoft Public License (Ms-PL)

Object - Collection - Binary File. Using serialization and deserialization to work with binary files based on Collections.

By Joe Blauth

In this tutorial we will take a closer look to a technology that allows you to store objects externally with ease, retrieve them effortless and work with them as if you were dealing with some kind of simple database.
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), .NET, Architect, Dev, Design
Posted:20 Nov 2008
Views:6,444
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.90 Rating: 3.00 out of 5
1 vote, 50.0%
1

2

3
1 vote, 50.0%
4

5

Objects - Collection - Binary File. Working with serialization and deserialization to store binary files from Collections.

Introduction

The most popular way to build applications that deal with structured data but do not use a classical database like a SQL-Server is to work with XML. Basically this is doing a fantastic job and if you 're familiar with XML in .net you can work with a powerful set of functions to deal with your stored data.

But there is more.

Using the code

In this tutorial we will take a closer look to a technology that allows you to store objects externally with ease, retrieve them effortless and work with them as if you were dealing with some kind of simple database.

We only do need four ingredients to build up our system:

  1. A class we can instantiate our objects from
  2. Some sort of collection to push these objects in – which is a hashtable in our case
  3. And a binary writer to actually save our data package to disc
  4. A binary reader to retrieve our data back

This actually might sounds more confusing than it is so we better jump right into it and take a look at some basic example.

Lets fire up VS 2005 and start a new command line application in VB.net.

Rename the initial module from “Module1” to “Binarywriter” and save the Project to a suitable location. We can now start to look closer to our subject.

1. The Data Class. First of all we add a new class called “cItem” [1] and give it a property.

Public Class cItem

    Private _dValue As Double

    Public Property Value()
        Get
            Return _ dValue
        End Get
        Set(ByVal Value)
            _ dValue = (Value * 1000)
        End Set
    End Property
End Class

Later we are going to derive objects from this class and store them in our collection to save them right away into a binary file.

2. The Collection. To fulfill the second requisite we switch back to our “BinaryWriter” Module and add some Import-Statements. In detail that is

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

After that we’re adding a Sub called “Serialize” in which we’re going to fill up our collection with a larger amount of randomized values. The single values are stored in Object-Instances of our cItem-Class.

    Private Sub Serialize()
        'define our Collection – a hashtable in our case
        'because it will hold all the Items we are going to
        'create, we will call it ItemCollection
        Dim oItemCollection As New Hashtable

        Dim oItem As cItem
        Dim iCounter As Integer

        'We will fill our Data-File with a mass of values
        For iCounter = 1 To 10000
            'Make new Item from cItems
            oItem = New cItem
            Randomize()
            'Give it a random Value
            oItem.Value = Rnd()
            'Add it to our collection
            oItemCollection.Add("Value" & iCounter, oItem)
            'destroy our Item
            oItem = Nothing
        Next
    End Sub

3. The Binary Writer. We are now ready to store our filled Collection as a binary file. Therefore we are using a FileStream and a BinaryFormatter to stream the Collection to disk.

    Private Sub Serialize()
        '(...)

        'As we want to write a binary file, we need to open a
        'FileStream to create one. That’s why we need the System.IO
        'Import for, btw.
        Dim oFileWriter As New FileStream("DataFile.dat", FileMode.Create)


        'Construct a BinaryFormatter and use it to serialize the data
        'to the stream. 
        Dim oFormatter As New BinaryFormatter
        Try
            oFormatter.Serialize(oFileWriter, oItemCollection)
        Catch e As SerializationException
            Console.WriteLine("Serialization failed. Reason: " & e.Message)
            Throw
        Finally
            oFileWriter.Close()
        End Try
    End Sub

4. The Binary Reader. To retrieve back our data from the DataFile we make use of Deserialization. Therefore we are adding a new Function to the BinaryFile-Module and name it to “Deserialize”. To hold up the retrieved data we initiate an empty hashtable. After that we are going mainly the same way back as we went to store the Collection to the binary file and make use of a FileStream in addition with a BinaryFormatter to actually access the data. After we cast the deserialized content of the DataFile we are finished.

    Private Sub Deserialize()
        Dim oItemCollection As Hashtable
        Dim oFileReader As FileStream
        Dim oFormatter As BinaryFormatter

        'Define our Collection. Corresponting to the Collection used
        'in the Serialize() –Function it’s a hashtable here.
        oItemCollection = New Hashtable
        oItemCollection = Nothing

        'We want to read from our binary file, so we initialize
        'a Filestream
        oFileReader = New FileStream("DataFile.dat", FileMode.Open)

        Try
            oFormatter = New BinaryFormatter

            oItemCollection = DirectCast(oFormatter.Deserialize(oFileReader), Hashtable)
        Catch e As SerializationException
            Console.WriteLine("Deserialize failed. Reason: " & e.Message)
            Throw
        Finally
            oFileReader.Close()
        End Try
    End Sub

We now have our data retrieved back in a hashtable.

Next Steps. It is said in the introduction that it would be possible to work with binary files as if you are working with some kind of simple database. This is a true statement with no doubt, but slightly incorrect verbalization because – as you might already noticed – we are not working with the DataFile itself but execute operations against the Collection which resides in memory.

This again is a great advantage because you can optimize your collection to your special needs.

But lets look at two basic examples of how to work with our above designed system and access some data.

Access Values. In this case you an make use of then DictionaryEntry and DirectCast the Entry to the Item-Class in order to access the actual value of the object.

    Dim oEntry As DictionaryEntry
    Dim iCounter As Integer = 0
    Dim iValue As Integer
 
    For Each oEntry In oItemCollection
        iValue = DirectCast(oEntry.Value, cItem).Value
 
        If iValue = iRandomNumber Then
            iCounter += 1
        End If
    Next
 
    Console.WriteLine("The value {0} was found {1} times", iRandomNumber, iCounter)

Result. This tutorial demonstrates how to easily store and retrieve data to/from binary files using a Collection-derivate and (de-)serialization.

To hold objects in a collection and serialize them offers some advantages in contrast to classical databases in case the amount of data is not growing to big. Among these advantages is the fact that the Data-File is stored binary, which obviously results in a restrictive access to the Data-File. Also it enabled fast and handy working with structured data because we are loading the complete data to memory, so every operation against the data is an operation in memory.

Also it was shown how easily the retrieval of binary data files could be.

With a wisely selected collection type and a properly designed Data-class you can make use of a lot of advantages to write simple, efficient and clean code which possible results in a robust application. A hashtable as a collection was only used for demonstration purposes. Basically you can use every type of collection (even your own ones). Together with the Data-Class you have plenty of tools to make your application deal properly with a binary file.

Remark. Unfortunately the method above does not apply for the .net compact framework, so you cannot use this method to store data on a mobile device.


[1] I know many people would say that “cItem” does not follow the usual naming convention in .net. I won’t veto on that but what I am doing here is following the naming conventions of my company. And thats not only because i was authoring it ;)

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Joe Blauth


Member

Location: Germany Germany

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberMember 423460122:01 28 Jun '09  
GeneralRe: My vote of 1 PinmemberJoe Blauth11:53 10 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Nov 2008
Editor:
Copyright 2008 by Joe Blauth
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project