Click here to Skip to main content
15,886,096 members
Articles / Programming Languages / Visual Basic
Article

Setting and retrieving a Dataset in a Hashtable

Rate me:
Please Sign up or sign in to vote.
2.15/5 (13 votes)
3 Aug 20041 min read 73K   439   23  
Beginner tutorial on settting and retrieving dataset values from a hashtable

Introduction

This is a kindergarten tutorial on how to add a dataset as a hashtable entry item and how to retrieve the values from the hashtable.

Background

I was looking out for a class in .NET that resembled the propertyBag. What a relief when I found this class on msdn.

Hashtable

Hashtable is a class in System.Collections. It is a collection of key-and-value pairs that are organized based on the hash code of the key.

The Code

The file in the source code contains a function GetHashTable() to set a dataset into a hashtable and return it to the calling function. First we create a new hashtable, like so
VB.NET
Dim htbl As System.Collections.Hashtable = New System.Collections.Hashtable()
The dataset is populated using a DataSet.ReadXml.
VB.NET
Dim ds As DataSet = New DataSet()
ds.ReadXml("SomeFile.xml")
Next we add the populated dataset onto the hashtable and return it. The syntax for the add is as follows
VB.NET
Public Overridable Sub Add(ByVal key As Object, ByVal value As Object) _
  Implements IDictionary.Add
The key is the element to add, and the value is the value of the key to add. So we add the dataset into the Hashtable and return the hashtable to the calling function.
VB.NET
htbl.Add("CustomerDetails", ds)
Return htbl
Now getting on to the calling function. We call GetHashTable() which will return a Hashtable. We now try to get the dataset back from the Hashtable. The Hashtable stores the key-value pairs based on the hash code of the key. This way, we can access the key value by doing a lookup of the key.
VB.NET
ds = htbl("CustomerDetails")
Now you got the dataset from the hashtable.

That was a very basic code!

The purpose of the article was to explain the concept behind adding in and retrieving objects from the hashtable. However the real purpose of using the hashtable goes much beyond this. This article is just to get u started up on Hashtables.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --