Showing posts with label Store Multiple object in Hash Table and access using Key. Show all posts
Showing posts with label Store Multiple object in Hash Table and access using Key. Show all posts

Friday, June 14, 2013

Store Multiple object in Hash Table and access using Key

Sometime it’s common requirement to store a collection of object in daily project development.We can use various technique to do that. Here I want to show how to use Hash Table to achieve this.And the most advantage of hashtable is you can access any object using it’s key. As in save time I have used PersonId as key of 
Hash Table.  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
 namespace BlogProject
{    class Person    {        public Int32 PersonID { get; set; }        public string Name { get; set; }         Hashtable Hs = new Hashtable();        public void SetData(Person p)        {            Hs.Add(p.PersonID, p);        }        public void ShowData(int Key)        {            Person objP =(Person) Hs[Key];            Console.WriteLine("Name:-"+ objP.Name);            Console.WriteLine("Person ID:-" + objP.PersonID);        }    }       class Program    {        static void Main(string[] args)        {            Person p = new Person();            p.PersonID = 100;            p.Name = "Sourav";             p.SetData(p);             p = new Person();            p.PersonID = 101;            p.Name = "Bapi";             p.SetData(p);            p.ShowData(101);              Console.ReadLine();        }    } }