Hashtable Class

   
Hashtable Class

Namespace

System.Collections

Createable

Yes

Description

A Hashtable object represents a collection of values (of type Object) that are indexed by objects called keys (also of type Object). We can also think of a hash table as containing key/value pairs.

Identification of the location of elements in a hash table is done using a hashing function . Simply put, a hashing function is a function that assigns a location in the hash table to each element, based on the element's value. This is not the place to go into any detail about hashing. It is worth mentioning that hash tables can be very efficient structures for storing and retrieving elements. However, there is no "best approach" to defining hashing functions, and so only experimentation can determine whether this particular implementation of a hash table is efficient in any given case.

Note that the Hashtable class is more flexible than the Collection class of the Microsoft.VisualBasic namespace.

Hashtable class members marked with a plus sign (+) are discussed in detail in their own entries.

Public Shared Method

Synchronized

Public Instance Properties

Count +
IsFixedSize
IsReadOnly
IsSynchronized
Item +
Keys +
SyncRoot
Values +

Public Instance Methods

Add +
Clear +
Clone
Contains
ContainsKey +
ContainsValue +
CopyTo +
Equals
GetEnumerator
GetHashCode
GetObjectData
GetType
OnDeserialization
Remove +
ToString

Example

The following example illustrates most of the members that we will discuss:

 Private Sub DoHashtable(  )     Dim i As Integer     Dim s(  ) As DictionaryEntry     Dim obj(  ) As Object     Dim icKeys As ICollection     ' Define a new hash table     Dim h As New Hashtable(  )     ' Add some elements to the hash table     h.Add("Be", "Beethoven")     h.Add("Ch", "Chopin")     h.Add("Mo", "Mozart")     h.Add("Sc", "Schubert")     ' Copy elements to an array of DictionaryEntry objects and display     ReDim s(h.Count)     h.CopyTo(s, 0)     For i = 0 To h.Count - 1         Console.WriteLine(s(i).Value)     Next     ' Show the keys     icKeys = h.Keys     ReDim obj(h.Count)     icKeys.CopyTo(obj, 0)     For i = 0 To h.Count - 1         Console.WriteLine(CStr(obj(i)))     Next     ' Does the hash table contain the value "Beethoven"     MsgBox("Beethoven: " & CStr(h.ContainsValue("Beethoven")))     ' Clear the hash table     h.Clear(  ) End Sub 

VB.NET/VB 6 Differences

The Hashtable object is new to the .NET platform.

See Also

Collection Class, Queue Class, Stack Class

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net