Friday, December 18, 2009

The Dictionary object in ASP.NET

The Dictionary Object

The Dictionary object is used to store information in name/value pairs (referred to as key and item). The Dictionary object might seem similar to Arrays, however, the Dictionary object is a more desirable solution to manipulate related data.

Comparing Dictionaries and Arrays:
· Keys are used to identify the items in a Dictionary object
· You do not have to call ReDim to change the size of the Dictionary object
· When deleting an item from a Dictionary, the remaining items will automatically

shift up
· Dictionaries cannot be multidimensional, Arrays can
· Dictionaries have more built-in functions than Arrays
· Dictionaries work better than arrays on accessing random elements frequently
· Dictionaries work better than arrays on locating items by their content

The following example creates a Dictionary object, adds some key/item pairs to it, and retrieves the item value for the key gr:

<%Dim dSet d=Server.CreateObject("Scripting.Dictionary")d.Add "re","Red"d.Add "gr","Green"d.Add "bl","Blue"d.Add "pi","Pink"Response.Write("The value of key gr is: " & d.Item("gr"))%>Output:The value of key gr is: Green

The Dictionary object's properties and methods are described below:

Properties
Property Description
CompareMode Sets or returns the comparison mode for comparing keys in a Dictionary object
Count Returns the number of key/item pairs in a Dictionary object
Item Sets or returns the value of an item in a Dictionary object
Key Sets a new key value for an existing key value in a Dictionary object

Methods
Method Description
Add Adds a new key/item pair to a Dictionary object
Exists Returns a Boolean value that indicates whether a specified key exists in the Dictionary object
Items Returns an array of all the items in a Dictionary object
Keys Returns an array of all the keys in a Dictionary object
Remove Removes one specified key/item pair from the Dictionary object
RemoveAll Removes all the key/item pairs in the Dictionary object


ll the keys and corresponding items in our dictionary object:
Key Item
green #00FF00
red #FF0000
aqua #00FFFF
yellow #FFFF00
white #FFFFFF
fuschia #FF00FF
black #000000
blue #0000FF

Number of key/item pairs in our dictionary: 8
Does a "blue" key exist: True
Value of the item corresponding to the "blue" key: #0000FF
Changing the value of the "blue" key's item. Its value is now: #000099
Changing the value of the "blue" key to "dark blue".
All the keys and corresponding items in our dictionary object:
Key Item
green #00FF00
red #FF0000
aqua #00FFFF
yellow #FFFF00
white #FFFFFF
fuschia #FF00FF
black #000000
dark blue #000099

Bind an ASP.NET DropDownList to a Dictionary
I have seen a lot of folks keeping data in a Dictionary<,>. However when it comes to binding this Dictionary with a control, they feel lost!

Here's a simple way that demonstrates how to bind a Dictionary to an ASP.NET DropDownList control



asp:DropDownList ID="ddlRelatives" runat="server"

/asp:DropDownList




C#



Dictionary dicRel = new Dictionary();

dicRel.Add(1, "Father");

dicRel.Add(2, "Mother");

dicRel.Add(3, "Brother");

dicRel.Add(4, "Sister");

dicRel.Add(5, "Others");

ddlRelatives.DataSource = dicRel;

ddlRelatives.DataTextField = "Value";

ddlRelatives.DataValueField = "Key";

ddlRelatives.DataBind();