Hashtable Class


Hashtable Class

Namespace

System.Collections

Creatable

Yes

Description

The Hashtable class implements a hash-based dictionary of value pairs. A hashtable represents a collection of values that are indexed by keys; these values and keys are associated in a dictionary of key/value pairs. A hashing function is used to speed up access to each pair. In .NET, both the values and the keys are objects of any type. The Hashtable class is somewhat more flexible than the standard VB Collection class.

The following table lists some of the more useful and interesting members of the Hashtable class. Those marked with an asterisk (*) have separate entries in this chapter.

Member

Description

Add Method *

Adds a key/value pair to a hashtable

Clear Method *

Removes all items from the hashtable

Clone Method

Makes a distinct copy of the hashtable and its members

Contains Method

Identical to the Hashtable.ContainsKey method

ContainsKey Method *

Indicates whether a specific key is in the hashtable

ContainsValue Method *

Indicates whether a specific value is in the hashtable

CopyTo Method *

Copies hashtable elements to an existing array

Count Property

Indicates the number of items currently in the hashtable

IsReadOnly Property

Indicates whether the hashtable is read-only or not

Item Property *

Retrieves a value from the hashtable based on its key

Keys Property *

Retrieves the collection of all keys in the hashtable

Remove Method *

Removes a value/key pair from the hashtable

Values Property *

Retrieves the collection of all values in the hashtable


Example

This example makes use of a simple hashtable.

     ' ----- Build a simple hashtable.     Dim nameHash As New Hashtable(  )     nameHash.Add("Be", "Beethoven")     nameHash.Add("Ch", "Chopin")     nameHash.Add("Mo", "Mozart")     ' ----- Select and remove items.     MsgBox(nameHash.Item("Be"))    ' Displays "Beethoven"     nameHash.Remove("Ch")          ' Removes "Chopin"     MsgBox(nameHash.Count)         ' Displays 2     nameHash.Clear(  ) 

See Also

Collection Class, Queue Class, Stack Class




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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