Thursday, September 15, 2011

Adding, Retrieving Name objects from a Hashtable in C#.Net

Hashtable is nothing but a collection of objects. To get brief idea,

Take a few instance of Name Class and add them to a Hashtable. Your Name will be value for Hashtable but you need to decide about key. key will unique in the Hashtable. When you are retrieving values from Hashtable this key place a major role.

First thing you need to add is namespace for Hashtable.

using System.Collections;

then take few instance of your class Name.

//Creating few instances for Class Name
Name emmu = new Name("Emmaneale", "Mendu");
Name vamshi = new Name("Vamshi", "Malyala");
Name pradeep = new Name("Pradeep", "Kandula");

We got our Values ready. So lets use initials as our key to insert into Hastable as shown below.

//Add Names into Hashtable
Hashtable SampleHashtbl = new Hashtable();
SampleHashtbl.Add("EM", emmu);
SampleHashtbl.Add("VM", vamshi);
SampleHashtbl.Add("PK", pradeep);

here if you try to use duplicates for key. You will get an error as shown in Figure 1.

 Figure 1
Done with inserting. Now lets retrieve them and display on our page.

 //Retrieve Names from Hastable
 Name ex = (Name)SampleHashtbl["EM"];   
 if (ex != null)
    Response.Write(ex.FullName + "<br/>");
 else
    Response.Write("NOT FOUND<br/>");

To retrieve we need key string. If it didn't found the key it returns null. So check before display.

That's a small example on Hashtable.

Here is the Complete Code and Output in Figure 2.

protected void Page_Load(object sender, EventArgs e)
    {
        //Creating few instances for Class Name
        Name emmu = new Name("Emmaneale", "Mendu");
        Name vamshi = new Name("Vamshi", "Malyala");
        Name pradeep = new Name("Pradeep", "Kandula");

        //Add Names into HashTable
        Hashtable SampleHashtbl = new Hashtable();
        SampleHashtbl.Add("EM", emmu);
        SampleHashtbl.Add("VM", vamshi);
        SampleHashtbl.Add("PK", pradeep);

        //Retrieve Names from HasTable
        Name ex = (Name)SampleHashtbl["EM"];   
        if (ex != null)
            Response.Write(ex.FullName + "<br/>");
        else
            Response.Write("NOT FOUND<br/>");

        ex = (Name)SampleHashtbl["VM"];
        if (ex != null)
            Response.Write(ex.FullName + "<br/>");
        else
            Response.Write("NOT FOUND<br/>");

        ex = (Name)SampleHashtbl["PK1"];
        if (ex != null)
            Response.Write(ex.FullName + "<br/>");
        else
            Response.Write("NOT FOUND<br/>");
    }

    public class Name
    {
        string FirstName;
        string LastName;

        //Constructor for our Class
        public Name(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }

        //public property for FullName
        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
    }

 Figure 2


Emmaneale Mendu
.Net Developer

1 comment:

  1. Really this is a good article, which helps a lot for beginners as me as well as developer. check this link...

    http://mindstick.com/Articles/e54db9d2-181f-4beb-a986-6f969ff3bd8d/?HashTable%20in%20C%20sharp

    Its also helped me in completing my task.
    Thanks Everyone!!

    ReplyDelete