O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required