Using Files as a Database

106 9
< Continued from page 1

The primary coding job is to add the code to make the buttons maintain the ListBox. The XML file format used to save the data in the program isn't changed at all. But to better organize a collection of XML files, I used a subdirectory for the collection. (This subdirectory will be, in effect, my "database.") A global constant was added to specify the location:

Const stockFileDir As String = "stockfiles"


The old StockCalc_Load event subroutine provides all of the code we need to read the XML file and the only change is to point to a different location for the XML file:

Dim Input As New XmlTextReader( _
   stockFileDir & "\" & Symbol & ".xml")


But since this routine will now be used in several other places, we place it in an internal subroutine and pass the name of the file that will be loaded as a parameter.

Private Sub LoadStockFromXml(ByVal Symbol As String)

In a very similar way, the old changeDefaults_Click gives us exactly what we will need to write the XML files and again, the only changes are to the subroutine declaration --

Private Sub AddXmlDocument(ByVal Symbol As String)

-- and the same code to build the file location in the statement that actually writes the XML file:

Dim Output As New XmlTextWriter( _
   stockFileDir & "\" & Symbol & ".xml", _
   System.Text.Encoding.UTF8)


The original StockCalc_Load event subroutine is now much simpler since all the work is done by subroutines:

' Populate the ListBox with
' all of the XML files in the folder
ListBoxLoad()
stockList.SelectedIndex = 0
stockSymbol.Text = stockList.Items(0)
' Load the form with the first item in the list
LoadStockFromXml(stockList.Items(0))


The ListBoxLoad subroutine starts off the processing now to find the names of all of the files in our "database" directory.

Dim fileList As _
   System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim slashPosition As Integer
Dim dotPosition As Integer
fileList = My.Computer.FileSystem.GetFiles( _
   stockFileDir, _
   FileIO.SearchOption.SearchTopLevelOnly, "*.xml")
stockList.Items.Clear()
For Each foundFile As String In fileList
   slashPosition = foundFile.LastIndexOf("\") + 2
   dotPosition = Len(foundFile) - 3 - slashPosition
   foundFile = _
      Mid(foundFile, slashPosition, dotPosition)
   stockList.Items.Add(foundFile)
Next


If you haven't seen them before, there are several items here that might cause a little confusion.

The first item that might be new to you is the use of the new Framework 2.0 feature, generics in the fileList collection. About Visual Basic has an introduction to generics in the article Generics! Cleaner Data - Faster Code!.

The fileList collection is populated using another new Framework 2.0 feature, My.Computer.FileSystem. You can read an introduction to the My namespace in What's New With Visual Basic .NET Express. This namespace is really just a treasure chest of tools like FileIO.SearchOption.SearchTopLevelOnly that we're using in this program designed to make your coding life faster and easier.

With just these few changes to the existing code, we're now ready to add the code for the four new buttons that maintain the ListBox and keep the form components in sync. Thanks to the new VB.NET 2005 My namespace, these subroutines are surprisingly simple (the standard event code has been left out to save space):

Private Sub changeStock_Click( ...
   AddXmlDocument(stockList.SelectedItem)
End Sub
Private Sub selectStock_Click( ...
   LoadStockFromXml(stockList.SelectedItem)
   stockSymbol.Text = stockList.Items(stockList.SelectedIndex)
End Sub
Private Sub addStock_Click( ...
   AddXmlDocument(stockSymbol.Text)
   ListBoxLoad()
   stockSymbol.Text = stockList.Items(stockList.SelectedIndex)
End Sub
Private Sub deleteStock_Click( ...
   My.Computer.FileSystem.DeleteFile( _
      stockFileDir & "\" & stockList.SelectedItem & ".xml")
   ListBoxLoad()
   stockList.SelectedIndex = 0
   stockSymbol.Text = stockList.Items(0)
   LoadStockFromXml(stockList.Items(0))
End Sub


Except for another tool from the My namespace - My.Computer.FileSystem.DeleteFile - these are mainly calls into the code that we've already completed. One of the big advantages of the design I selected was that I didn't have to recode everything using a different technology. I could just add to what I already had.

The complete code for the updated StockCalc application can be downloaded here.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.