Hashtable.Item Property

   
Hashtable.Item Property

Class

System.Collections.Hashtable

Syntax

   hashtablevariable   .Item(   key   ) 
hashtablevariable (required; Hashtable object)

A reference to a Hashtable object

key (required; Object)

The key whose value is to be retrieved

Return Value

An Object representing the value associated with key

Description

Returns an Object that is the value associated with a particular key/value pair.

Rules at a Glance

  • Item is the default property of the Hashtable object, and since it is parameterized, we can write:

       hashtablevariable   (   key   ) 
  • Item is a read/write property. In other words, you can use the Item property to retrieve the value belonging to a particular key, as well as to modify the value belonging to a particular key.

  • If key does not exist in the hash table when you attempt to retrieve a value, the Item property returns Nothing .

  • If key does not exist in the hash table when you attempt to modify a value, the key and its associated value are added to the hash table, as a sort of implicit add. For example, if the key "AK" does not exist in a hash table, the code fragment:

     hshStates.Item("AK") = "Alaska" 

    adds the key "AK" and its associated value, "Alaska" .

Programming Tips and Gotchas

  • To guard against inadvertently adding a member to the hash table when you intend to modify an existing value, call the ContainsKey method beforehand.

  • You can also retrieve individual members of the Hashtable object by iterating it using the For Each...Next statement. Each iteration of the loop returns a DictionaryEntry object containing a single key/value pair. For information on the DictionaryEntry object, see the entry for the Hashtable.CopyTo method.

   


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