Ask the Expert

Setting a pointer to an item in a .NET collection

When working with a collection in .NET, how do I set a pointer to an item within the collection? In VB6, used to be able to just:

    Requires Free Membership to View

Set oColItem = oCollection(sKeyString)

All examples I've seen to this point make you "For Each" and loop through the entire collection. Is there a better way?

It sounds like what you want to use is a Hashtable. A Hashtable implements ICollection and allows retrieval of items by their key.

To use a Hashtable you insert values like this:

' Here we'll create a hashtable of employee numbers and employee names
Dim table As Hashtable = New Hashtable()

' now, we will add elements to the hashtable, as key-value pairs
table.Add(5123, "Jay")
table.Add(1829, "Tom")
table.Add(2882, "Matt")

You retrieve values thus:

// SEARCHING BY VALUE. Use the method ContainsValue
String valueToFind = "Jay";

if (table.ContainsValue(valueToFind)) {
Console.WriteLine("Found {0} in the list.", valueToFind );
}

// SEARCHING BY KEY. Use the method Contains
int keyToFind = 5123;

if (table.Contains(keyToFind)) {
Console.WriteLine("Found {0} in the list.", keyToFind);
}

The following guidelines are from MSDN:

Be sure to choose your Collections class carefully. Using the wrong collection can restrict your use of the collection.

Consider the following questions:

  • Do you need a sequential list where the element is typically discarded after its value is retrieved?
    • If yes, consider Queue or Stack.
    • If not, consider the other collections.
  • Do you need to access the elements in a certain order, such as first-in-first-out, last-in-first-out, or randomly?
    • Queue offers first-in-first-out access.
    • Stack offers last-in-first-out access.
    • The rest of the collections offer random access.
  • Do you need to access each element by index?
    • ArrayList and StringCollection offer access to their elements by the zero-based index of the element.
    • Hashtable, SortedList, ListDictionary, and StringDictionary offer access to their elements by the key of the element.
    • NameObjectCollectionBase and NameValueCollection offer access to their elements by either the zero-based index or the key of the element.
  • Will each element contain one value, a combination of one key and one value, or a combination of one key and multiple values?
    • One value: Use any of the collections based on IList.
    • One key and one value: Use any of the collections based on IDictionary.
    • One key and multiple values: Use the NameValueCollection class in the System.Collections.Specialized Namespace namespace.
  • Do you need to sort the elements differently from how they were entered?
    • Hashtable sorts the elements by the hash code of the key.
    • SortedList sorts the elements by the key, based on an IComparer implementation.
    • ArrayList provides a Sort method that takes an IComparer implementation as a parameter.
  • Do you need fast searches and retrieval of information?
    • ListDictionary is faster than Hashtable for small collections (10 items or less).
  • Do you need collections that accept only strings?
    • StringCollection (based on IList) and StringDictionary (based on IDictionary) are in the System.Collections.Specialized namespace.

This was first published in September 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: