SIGN UP MEMBER LOGIN:    
ARTICLE

Dictionary Class in C#

Posted by Hussein Ahmed Articles | C# Language December 28, 2010
The Dictionary class gives us the opportunity to map some values to specific keys; in real life scenarios we need this mechanism to represent some values whenever we call the key associated with them according to an event.
Reader Level:



Introduction

The Dictionary class gives us the opportunity to map some values to specific keys; in real life scenarios we need this mechanism to represent some values whenever we call the key associated with them according to an event.

The following code snippet maps different keys of countries to show their values represented by avaliable universities in every chosen country.

First we have to import specific namespace which is; using System.Collections.Generic. Secondly, we decalre Countrylist of type Dictionary class represented by string keys and an array of string values, then in the Form1_Load event we populate the Countrylist with appropriate data.

Thirdly, we use countrycomboBox to populate every key in our Countrylist, and according to the choice of every key, the available universities will be shown in lisbox1 via firing countrycomboBox_SelectedIndexChanged event.

Dictio.gif

using System;
using System.Collections.Generic;
using System.Windows.Forms;

 namespace DictionaryClass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

// Creat Dictionary <Key, Value>  Pairs; Key represented by Type of string while Value represented by  an array of string

        Dictionary<string, string[]> CountryList = new Dictionary<string, string[]>();

        private void Form1_Load(object sender, EventArgs e)
        {

// The following snippet shows every key with its suitable values

            CountryList["Egypt"] = new string[]{"Cairo University", "Ain Shams University","Assuit University"};
            CountryList["Saudia Arabia"]= new string[]{"Alqassam University","Altaef University","King Saud University" };
            CountryList["Malaysia"] = new string[] { "UUM University","USM University","UKM University"};

//Expose countrycomboBox to host keys

            countrycomboBox.Items.Add("--Please Select--");

            foreach (string CountryKey in CountryList.Keys)
            {
                countrycomboBox.Items.Add(CountryKey);
            }
            countrycomboBox.SelectedIndex = 0;
        }
 
        private void countrycomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedCountry = countrycomboBox.SelectedItem.ToString();

            if (countrycomboBox.SelectedIndex==0)
            {
                listBox1.Items.Clear();
            }
            else
            {

//Expose listBox1 to host values associated with its key.

            listBox1.Items.Clear();
            listBox1.Items.AddRange(CountryList[selectedCountry]);
            }
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor