Posts Tagged ‘key’

Enumerate Through Dictionary C#

Continuing from a previous post on Using C# Dictionary Collection, the next most common tasks is enumerate through every element in the Dictionary. To begin, each element in a Dictionary is a KeyValuePair. According to the C# documentation for Dictionary, the order of a KeyValuePair enumeration is not defined. Therefore, the KeyValuePair order may not be the same as the insertion (or Add) order. For that reason, we cannot use a for loop. Instead, a foreach loop is our solution. In this tutorial, I will demonstrate how to loop through the KeyValuePair, the Key, and the Value of a dictionary.

Foreach KeyValuePair

In this first example, we will be looping through the KeyValuePair. We can get both the key (as an int) and the value (as a string) from the KeyValuePair. Note that the type of the KeyValuePair must be declared in the foreach loop.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (KeyValuePair pair in myDictionary)
{
    int key = pair.Key;
    string value = pair.Value;
    Debug.WriteLine("Key: " + key.ToString() + " Value: " + value);
}

Foreach Key

In this second case, we will be looping through all Keys in the Dictionary. The Keys must be unique and in this example, the key is an integer.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (int key in myDictionary.Keys)
{
    Debug.WriteLine("Key: " + key.ToString());
}

Foreach Value

In this final case, we are looping through the Value of our dictionary. The value can be anything you define, but in our example, it is a string.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (string value in myDictionary.Values)
{
    Debug.WriteLine("Value: " + value);
}

Using C# Dictionary Collection

A Dictionary object in C# has become one of my recent favorites. Below are a few important aspects:

  1. Requires including System.Collections.Generic;
  2. Each entry in a Dictionary is a KeyValuePair (consisting of two objects: key and value)
  3. The key must be unique
  4. The value does not need to be unique
  5. Both the key and value can be any object (string, int, custom class, etc…)
  6. Retrieving a value via a key take close to O(1) time
  7. The order of a KeyValuePair enumeration is not defined

Next is an example of how to create and initialize a Dictionary object. Our Dictionary will have an integer and as a key and a string as a value.

Dictionary<int, string> myDictionary = new Dictionary<int, string>();

Now that we have our Dictionary datatype, we will add two values to the Dictionary. Note that at this point, intellisense knows the two expected types.

myDictionary.Add(1, "Victor");
myDictionary.Add(2, "Billy");

Finally, deleting an object is just as easy:

myDictionary.Remove(1);

And these are the basics to using a Dictionary collection!

Form Submit On Enter in Visual Studio 2008

Until recently, I had always used javascript to allow the enter key to submit a form (as seen in a previous posting called Submit Form On Enter Key Solution). Since then, I was queued into a GUImethod of controlling enter key form submissions in Visual Studio 2008 (and I’m sure this works in other versions as well).

How to submit a form in Visual Studio 2008To do this in the GUI style, click on the form of the page and then select the elements properties. Under the ASP.NET subheading, there is a form property called DefaultButton. Insert the button control id of the default button. This will essentially submit the form on enter (by performing the action on the specified button). The image to the left is an example of the form property section with a default button set to btnSetCookie. Note that for one reason or another, intellisense is not enabled on this property and you will have to manually type in the button control id.

Of course, you are not constrained to doing this in the GUI style. To submit a form on enter programmatically, simply find the form and insert the keyword defaultbutton=”BUTTON_CONTROL_ID_HERE”. Below is an example of how the above example would look programmatically.

<form id="frmTestForm" runat="server" defaultbutton="btnSetCookie">