Hashtable Serialized Application In C#


The application is self explanatory, and isvery depth in various aspects. It is basically a hashtable serializedapplication in C#.

using System;

using System.Collections;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

namespace ConsoleApplication5

{

[Serializable]

struct name

{

public string forname;

public string familyname;

}

[Serializable]

struct address

{

public string street;

public int number;

}

class clsDataBlock

{

public static Hashtable NameToAddress = new Hashtable ();

[STAThread]

static void Main(string[] args)

{

clsDataBlock db=new clsDataBlock ();

string ans="";

while("x" != ans)

{

System.Console.WriteLine ("1 to enter new");

System.Console.WriteLine ("2 to search");

System.Console.WriteLine ("3 to save (serialize)");

System.Console.WriteLine ("4 to load (deserialize)");

System.Console.WriteLine ("5 to show data");

System.Console.WriteLine ("x to quit");

System.Console.WriteLine ("———————–");

ans = System.Console.ReadLine ();

switch(ans)

{

case "1":db.AddNew();

break;

case "2":db.Search();

break;

case "3":db.Save();

break;

case "4":db.Load();

break;

case "5":db.Show();

break;

}

}

}

public void AddNew()

{

name nm;

address ad;

string ans;

System.Console.WriteLine ("enter first name (x to exit):");

ans = System.Console.ReadLine();

nm.forname = ans;

System.Console.WriteLine ("enter last name (x to exit):");

ans = System.Console.ReadLine();

nm.familyname = ans;

System.Console.WriteLine ("enter street name (x to exit):");

ans = System.Console.ReadLine();

ad.street = ans;

System.Console.WriteLine ("enter street number (x to exit):");

ans = System.Console.ReadLine();

ad.number = int.Parse (ans);

NameToAddress.Add(nm,ad);

}

public void Search()

{

name nm2;

string ans2;

System.Console.WriteLine ("enter first name:");

ans2 = System.Console.ReadLine ();

nm2.forname = ans2;

System.Console.WriteLine ("enter last name:");

ans2 = System.Console.ReadLine ();

nm2.familyname = ans2;

System.Console.WriteLine ("working…");

object sname = NameToAddress[nm2];

if (sname != null)

{

address ad2 = (address)sname;

System.Console.WriteLine(" address: "+ ad2.street + " " + ad2.number );

}

else

System.Console.WriteLine ("no name like that…");

}

public void Save()

{

FileStream fs = new FileStream ("Store.dat",FileMode.OpenOrCreate ,FileAccess.Write );

try

{

Hashtable a = new Hashtable();

a = NameToAddress;

BinaryFormatter bf=new BinaryFormatter ();

bf.Serialize (fs,a );

}

finally

{

fs.Close ();

}

}

public void Load()

{

FileStream fs = new FileStream ("Store.dat",FileMode.Open ,FileAccess.Read );

try

{

Hashtable a = new Hashtable();

BinaryFormatter bf=new BinaryFormatter ();

a=(Hashtable)bf.Deserialize (fs);

NameToAddress = a;

}

finally

{

fs.Close ();

}

}

public void Show()

{

name nm3;

address ad3;

foreach (DictionaryEntry entry in NameToAddress )

{

nm3 = (name)entry.Key;

ad3 = (address)entry.Value;

Console.WriteLine ("name= {0} {1}, address= {2} {3}",

nm3.forname,nm3.familyname ,ad3.street ,ad3.number);

}

}

}

}

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!