MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Understanding the SortedDictionary Functionality using Csharp and ASP.NET

In this article you will understand how to work with SortedDictionary, which works similar with Dictionary.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction:

SortedDictionary work in same way as Dictionary, Only difference is that it keeps the keys always sorted. So if an application has a sorted data requirement, it can be good option with less complexity.

This article will cover the following topics:

  • SortedDictionary - Add Item
  • SortedDictionary - Delete Item
  • SortedDictionary - Search by Key
  • SortedDictionary - Search by Value
  • SortedDictionary - Display Data
  • SortedDictionary - Other Functionality

Initial UI and SortedDictionary with Smart Phone Data

Figure 1: Initial UI and SortedDictionary with Smart Phone Data

ASP.NET Script:

ASP.NET script is used to properly demonstrate the SortedDictionary functionality. The below script have the following items:

  • ASP.NET TextBox Controls
  • ASP.NET Label Controls
  • ASP.NET Buttons Controls

Listing 1: ASP.NET Script for SortedDictionary Demonstration

<head runat="server">
    <title>MrBool.com - SortedDictionary Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        SortedDictionary Item:
        <asp:TextBox ID="txtSortedDictionaryKey" runat="server"></asp:TextBox> 
        <asp:TextBox ID="txtSortedDictionaryValue" runat="server"></asp:TextBox>
        <br />
        <br />
        Message:
        <asp:Label ID="lblSortedDictionaryDisplay" runat="server" Text=""></asp:Label>
        <br />
        <br />
        SortedList State:
        <asp:Label ID="lblSortedDictionaryState" runat="server" Text=""></asp:Label>
        <br />
        <br />
         <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
         <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
         <asp:Button ID="btnSearchbyKey" runat="server" Text="Search by Key" OnClick="btnSearchbyKey_Click" />
         <asp:Button ID="btnSearchbyValue" runat="server" Text="Search by Value" OnClick="btnSearchbyValue_Click" />
    </div>
    </form>
</body> 

C# Code:

C# provide a separate collection class to handle the functionality of SortedDictionary. The SortedDictionary class is inside the Collections.Generic namespace so you have to include the following namespaces to use the SortedDictionary class.

Listing 2: Including namespaces

using System;
using System.Collections.Generic;

Now create the SortedDictionary by

Listing 3: Creating SortedDictionary

static SortedDictionary<string, string> SamartPhoneSortedDictionary; 

Now initialize it by

SamartPhoneSortedDictionary = new SortedDictionary<string, string>(); 

Note that we are using the static object so that we can use it through out the program execution. This SortedDictionary is type of <string, string>, means key is string and value part is also string.

SortedDictionary - Add Item:

To let you know that we are using the Smart Phone data for this demonstration. The add item functionality is same as in other collection classes. Use the Add() method to add the key and value pair and once you add any item it will be automatically sorted.

Add Item - A Key, Value Pair Added

Figure 2: Add Item - A Key, Value Pair Added (“Nokia Lumia 800”, “Seven”)

Listing 4: SortedDictionary - Add Item Source Code

protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtSortedDictionaryKey.Text.Trim() != "" && txtSortedDictionaryValue.Text.Trim() != "")
        {
            if (!SamartPhoneSortedDictionary.ContainsKey(txtSortedDictionaryKey.Text.Trim()))
            {
                SamartPhoneSortedDictionary.Add(txtSortedDictionaryKey.Text.Trim(), txtSortedDictionaryValue.Text.Trim());

                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Added";
            }
            else
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary - Key Already Exist";
            }
        }
        else
        {
            lblSortedDictionaryDisplay.Text = "SortedDictionary - Please Enter an Item to Add";
        }
        DisplayData();
    }
 

SortedDictionary - Delete Item:

To delete an item from SortedDictionary use the Remove() method. Pass the key in this method and it will delete the item from dictionary. After deletion the dictionary values will sort again.

Delete Item

Figure 3: Delete Item - (“IPhone 3”,”Two”) is removed from SortedDictionary

Listing 5: SortedDictionary - Delete Item Code

protected void btnDelete_Click(object sender, EventArgs e)
    {

        if (txtSortedDictionaryKey.Text.Trim() != "")
        {
            if (SamartPhoneSortedDictionary.ContainsKey(txtSortedDictionaryKey.Text.Trim()))
            {
                SamartPhoneSortedDictionary.Remove(txtSortedDictionaryKey.Text.Trim());

                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Deleted";
            }
            else
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary - Item Not Found.";
            }
        }
        else
        {
            lblSortedDictionaryDisplay.Text = "SortedDictionary - Please Enter an Item to Delete";
        }
        DisplayData();
    }
 

SortedDictionary - Search by Key:

SortedDictionary provide search by both key and value. To search by key use ContainsKey() method and pass the key value in it.

Search by Key

Figure 4: Search by Key - The ‘Samsung Note’ found in SortedDictionary key

Listing 6: SortedDictionary - Search by Key - Code

protected void btnSearchbyKey_Click(object sender, EventArgs e)
    {
        if (txtSortedDictionaryKey.Text.Trim() != "")
        {
            if (SamartPhoneSortedDictionary.ContainsKey(txtSortedDictionaryKey.Text.Trim()))
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Found";
            }
            else
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Not Found.";
            }
        }
        else
        {
            lblSortedDictionaryDisplay.Text = "Please Enter an Item to Search";
        }
        DisplayData();
    }
 

SortedDictionary - Search by Value:

Use ContainsValue to search a value in the SortedDictionary. The other option is to loop through all the items using foreach.

Search by Value

Figure 5: Search by Value - The value ‘One’ found in SortedDictionary

Listing 7: SortedDictionary - Search by Value - Code

protected void btnSearchbyValue_Click(object sender, EventArgs e)
    {


        if (txtSortedDictionaryValue.Text.Trim() != "")
        {
            if (SamartPhoneSortedDictionary.ContainsValue(txtSortedDictionaryValue.Text.Trim()))
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Found";
            }
            else
            {
                lblSortedDictionaryDisplay.Text = "SortedDictionary Item Not Found.";
            }
        }
        else
        {
            lblSortedDictionaryDisplay.Text = "Please Enter an Item to Search";
        }
        DisplayData();
    } 

SortedDictionary - Display Data:

Similar to dictionary, SortedDictionary use foreach loop to display data in SortedDictionary because the SortedDictionary is collection class.

Listing 8: SortedDictionary - Loop through items and display data

private void DisplayData()
    {
        lblSortedDictionaryState.Text = "";

        foreach (KeyValuePair<string, string> pair in SamartPhoneSortedDictionary)
        {
            lblSortedDictionaryState.Text += "," + pair.Key + ":" + pair.Value;
        }
        lblSortedDictionaryState.Text = lblSortedDictionaryState.Text.TrimStart(',');
    } 

SortedDictionary - Other Functionality:

Apart from above functionalities C# SortedDictionary provide some more common functions.

  • TryGetValue: Get the value if key exist and will not throw exception incase the key does not exist.
  • Count: Can get the item count
  • Clear(): Can remove all the values from dictionary
  • Value Looping: You cn loop through all the values in SortedDictionary
  • Key Looping: You can loop through all the keys in SortedDictionary

Listing 9: SortedDictionary - Other operation

 
private void OtherFunctionality()
    {
        string strValue = "";
        lblSortedDictionaryDisplay.Text = " Get the Value for a Key: " + SamartPhoneSortedDictionary.TryGetValue(txtSortedDictionaryKey.Text.Trim(), out strValue);

        //Clear all the items of SortedDictionary
        SamartPhoneSortedDictionary.Clear();

        lblSortedDictionaryDisplay.Text = " Count of All the Items of SortedDictionary: " + SamartPhoneSortedDictionary.Count;

        //lblSortedDictionaryDisplay.Text = " Capacity of All the Items of SortedDictionary: " + SamartPhoneSortedDictionary.Capacity;

        //value looping
        foreach (string str in SamartPhoneSortedDictionary.Values)
        {
            lblSortedDictionaryState.Text += "," + str;
        }

        //key looping
        foreach (string str in SamartPhoneSortedDictionary.Keys)
        {
            lblSortedDictionaryState.Text += "," + str;
        }

    } 

Conclusion:

That’s it from my side on Sorted Dictionary, I hope I have covered almost all common features in this article. We will cover other collection classes in next few tutorials.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login