C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Working with HashTable


Posted Date: 21 Mar 2004    Resource Type: Articles    Category: .NET Framework

Posted By: Sadha Sivam       Member Level: Gold
Rating:     Points: 10



Working with HashTable

Introduction

Collection represents a set of objects that can be access by stepping through each element in turn.
What is the big deal ?. Even an array can hold a set of objects.

True, but we have more flexibility when we use a collection object when compared to arrays.


  • Array are of fixed size, yes, it can be resized using Redim and Preserve. But for a Collection we can keep on adding elements to it.


  • Arrays can store only one data type(Other that object array) whereas collections can hold any objects.


  • Accessing the element is very simple and very fast.


  • Removing the element in Collection is very simple, in the case of arrays we need to shit the entire set of value up and reduce the size.


We have many collection class available in .Net like ArrayList, HashTable etc., in the System.Collections Namespace.

In this section of article we will see more on HashTable.

HashTable
Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Constructor of HashTable
We have ten overloaded Contructor. we will see some important constructors of HashTable.

  • Constructor with no parameters - Creates an empty HashTable.

  • Constructor with an Integer Parameter - Creates an empty hashtable, where the parameter represents the approximate number of element that the HashTable can have initially.




Adding an element to the HashTable
Adding an element to the hash table is very simple. It is done through Add method which takes the two parameter key and the value. Where the key represents the key which will be used to access the element and value is of type object which is the actual element to tbe stored.

Syntax

{hash table object}.Add(Key as Object, value as Object)



Accessing an element in the HashTable
Accesing the element in a HashTable is very simple. It can be accessed through the key which is given at the time of adding the element.
It is accessed through the parameterised default property item which takes an Key of type object and returns object contained in the HashTable with the specified key.

Syntax

{hash table object}.Item({key})

Or

{hash table object}({Key})


Count Property
The count property is used to get the size of the HashTable.

Searching for an element
It is very simple to search an object in the HashTable. We can search for an element using the Key associated with the object using the Contains methods which takes the key as the parameter and return boolean value(true representing the key is found, false representing the key is not found).

Clear Method
The clear method is used to clear all the elements stored in the Hash Table and empties the Hash table.

Deleting a Particular element
An element can also be removed very easily using the Remove method which takes a Key object as the parameter and remove the element associated with the key.

Syntax

{HashTable object}.Remove(Key as object)



Example, (Using the Hash Table)
In this example I am going to store the Employee information into a collection.


Dim ht as new HashTable()
ht.Add("Eno",10)
ht.Add("Ename","Sadha sivam")
ht.Add("EAge",20)


Now to access the Employee name we can use the item property as follows.


ht.Item("Ename")




Note
This hold good for both C#, VB.Net


Please send me your suggestion and feedback to sadhasivam1981@yahoo.com.

Please vist http://sadhasivam.t35.com






Responses

Author: komirishetty srinath    02 Jul 2004Member Level: Bronze   Points : 0
ur aricles are help me to acquire new skills to add up to my IT knowledge

thank you sir,


Author: Pat Tormey    03 Oct 2004Member Level: Bronze   Points : 0
You probably meant SHIFT (g)
"we need to shit the entire set of value up and reduce the size."
and maybe "values"






Author: Vidya Dharan    11 Oct 2004Member Level: Bronze   Points : 0
The tutorial is indeed very simple and easy to start working on HashTable


Author: Roopesh Babu Valluru    13 Jun 2008Member Level: Gold   Points : 0
thx a alot man....clean and simple xplanation....


Author: sunil    18 Jun 2008Member Level: Bronze   Points : 0
this is better articles---sunil


sunil.txt
Author: Gaurav Agrawal    18 Jun 2008Member Level: Silver   Points : 2
Hello Mam,
your aticle is very informative.
It provides both practical and theoritical knowledge.
Good one.
It is very helpful for beginers.
Keep posting.
Happy programming.

Thanks
Gaurav Agrawal


Author: Gaurav Agrawal    18 Jun 2008Member Level: Silver   Points : 2
Hello Mam,
your aticle is very informative.
It provides both practical and theoritical knowledge.
Good one.
It is very helpful for beginers.
Keep posting.
Happy programming.

Thanks
Gaurav Agrawal


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Handling Events in .Net - Part II
Previous Resource: Tips in VB.NET - Tip #1
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use