Recipe 14.25. Using Generic Collections


Problem

You need to store some objects in a collection, but you want to ensure that the collection allows only objects of a specific type.

Solution

Use one of the generic collections made available in .NET. They are called "generic" because they are data-typed generically, allowing you to replace nonspecific data-type placeholders with your own specific data types. ("Specifics" might have been a better name.) All generic collection classes appear in the System.Collections.Generic namespace.

As an example, the following code creates a stack (represented by the System.Collections.Generic.Stack class) that stores only Date objects. It then adds items to the stack:

 Dim dateStack As _    New System.Collections.Generic.Stack(Of Date) dateStack.Push(Today) dateStack.Push(DateAdd("d", 28, Today)) 

Discussion

The System.Collections.Generic namespace includes several useful generic collections for your use:


Dictionary(Of TKey, TValue)

This class implements a basic lookup system, with value objects made available through unique keys. You can indicate the data types of both the key and the value at declaration; they can be different. This class stores items in the dictionary through the related KeyValuePair(Of TKey, TValue) class.


LinkedList(Of T)

This class implements a doubly linked list, with immediate access to the first and last items in the list. Each list itemimplemented through the related LinkedListNode(Of T) classincludes a Previous and Next link to make traversal possible.


List(Of T)

This class implements a simple list of objects, providing access to items by index number. It includes methods to add, insert, and remove objects. It also includes many methods that locate items already in the list.


Queue(Of T)

This class represents a generic queue of objects, a "First In, First Out" (FIFO) construct. Items are added to the queue through the Enqueue( ) method and later retrieved and removed from the queue with the Dequeue( ) method. The Peek( ) method retrieves the oldest object from the queue but does not remove it.


SortedDictionary(Of TKey, TValue)

This class implements a basic lookup system, with value objects made available through unique keys. It also keeps the records sorted using a binary search tree. You can indicate the data types of both the key and the value at declaration; they can be different. If the TKey data type implements the IComparer interface, that type's comparison rules are used for the sort. This class stores items in the dictionary through the related KeyValuePair(Of TKey, TValue) class.


SortedList(Of TKey, TValue)

This class implements an ordered list. Items in the list are sorted by key as they are added. It is identical to the SortedDictionary(Of TKey, TValue) class, but it is optimized for fast insertion of previously sorted data. If the TKey data type implements the IComparer interface, that type's comparison rules are used for the sort. This class stores items in the dictionary through the related KeyValuePair(Of TKey, TValue) class.


Stack(Of T)

This class represents a generic stack of objects, a "Last In, First Out" (LIFO) construct. Items are added to the stack through the Push( ) method and later retrieved and removed from the stack with the Pop( ) method. The Peek( ) method retrieves the top-most object from the stack, but does not remove it.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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