View Single Post
Old Jan 6th, 2006, 9:05 AM   #15
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Quote:
Originally Posted by emdiesse
(I would love to get this all done in VB.NET will really help me when I go to uni because I will have more knowledge of a higher level language .)
Uni will no doubt consist of Hakell, Java and possibly some C

Sorry if my post was confusing; basically the simplest way to store data in a program without using a DBMS like MySQL is in objects, also encouraging good Object Oriented practices - XML Serialization is merely a convenient way to save and load them from disk, with the advantage that unlike conventional Binary Serialization, XML files are human readable.

For instance the following console app demonstrates saving and reloading a hashtable to and from a file:

' N.B. You'll need to add a reference to System.Runtime.Serialization.Formatters in your project
Option Explicit On 
Option Strict On
Imports System.Data
Imports System.IO
Imports System.Runtime.Serialization.Formatters
Module modMain

    Sub Main()
        Dim CapitalCities As Hashtable = New Hashtable
        Dim InputBuffer As Object

        With CapitalCities 'Enter some data into the hashtable
            .Add("England", "London")
            .Add("France", "Paris")
            .Add("Japan", "Tokyo")
            .Add("USA", "Washington")
            .Add("Liechtenstein", "Vaduz")
        End With

        ' Save it to disk
        SaveSomething(CType(CapitalCities, Object), Directory.GetCurrentDirectory() & "CapitalCities.xml")

        ' Clear out the object to prove it's being reloaded from disk
        CapitalCities = Nothing

        ' Load hashtable from disk
        LoadSomething(InputBuffer, Directory.GetCurrentDirectory() & "CapitalCities.xml")

        CapitalCities = CType(InputBuffer, Hashtable) ' We needed some memory to write to
        InputBuffer = Nothing

        Dim CapitalCitiesEnum As IDictionaryEnumerator = CapitalCities.GetEnumerator
        With CapitalCitiesEnum
            Do While .MoveNext
                ' List contents
                Console.WriteLine("The capital of " & CType(.Key, String) & " is " & CType(.Value, String))
            Loop
        End With

        Console.Write("Press enter...") : Console.ReadLine()
    End Sub

    Public Sub LoadSomething(ByRef SomeInputBuffer As Object, ByVal FilePath As String)
        Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
        Dim XMLStream As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Try
            SomeInputBuffer = XMLSerializer.Deserialize(XMLStream)
        Finally
            XMLStream.Close()
        End Try
    End Sub
    Public Sub SaveSomething(ByRef SomeOutputBuffer As Object, ByVal FilePath As String)
        Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
        Dim XMLStream As FileStream = New FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)
        Try
            XMLSerializer.Serialize(XMLStream, SomeOutputBuffer)
        Finally
            XMLStream.Close()
        End Try
    End Sub
End Module
Rory is offline   Reply With Quote