Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Initialize - add and clear the ordered dictionary using OrderedDictionary class – vb.NET - C# .NET.

Author: Maliha Atteeq
Download Source Code : 985_orderedDictionary.zip

In this simple article you will learn that how we can use, initialize, add and remove from ordered dictionary using OrderedDictionary class in vb and c#.net.

 Ordered Dictionary:

Ordered dictionary is used to represent the key-value pair. And you can access the member of ordered dictionary by index or key based   on your requirements.

First of all I will discuss in my article that how can we initialize the object of ordered dictionary.

Initialization:

In order to initialize the object of ordered dictionary, ordered dictionary provides us four overloaded constructors. I will discuss the most common and used constructors.

First constructor:

The first overloaded constructor is blank constructor. This constructor takes no argument as a parameter and builds the object of ordered dictionary with default initial size.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void button1_Click(object sender, EventArgs e)

        {

            System.Collections.Specialized.OrderedDictionary obj = new System.Collections.Specialized.OrderedDictionary();

            MessageBox.Show("ordered dictionary is build by using blank constructor");

        }

VB:

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim obj As New System.Collections.Specialized.OrderedDictionary()

        MessageBox.Show("ordered dictionary is build by using blank constructor")

    End Sub

Second constructor:

The second overloaded constructor takes one integer argument as a parameter which is the capacity of the ordered dictionary. And build the object aith that specified initial capacity.

To demonstrate make a new window application. Drag one button and one text box  on from.

Now write the following code on Button click event:

C#:

private void button1_Click_1(object sender, EventArgs e)

        {

            int capacity;

            capacity = Convert.ToInt16(txt_caps.Text);

            System.Collections.Specialized.OrderedDictionary obj = new System.Collections.Specialized.OrderedDictionary(capacity);

            MessageBox.Show("ordered dictionary is build with capacity "+Convert.ToString(capacity));

        }

VB:

Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)

        Dim capacity As Integer

        capacity = Convert.ToInt16(txt_caps.Text)

        Dim obj As New System.Collections.Specialized.OrderedDictionary(capacity)

        MessageBox.Show("ordered dictionary is build with capacity " & Convert.ToString(capacity))

    End Sub

Add a key-value pair:

In order to add a key-value pair in ordered dictionary, ordered dictionary class provides us a method named as add (). This function takes two arguments as parameter which are the values for key and value. This function returns nothing because its return type is void.

To demonstrate make a new window application. Drag one button and two text boxes on from.

Now write the following code on Button click event:

C#:

private void btn_add_Click(object sender, EventArgs e)

        {

            int key;

            int value;

            key = Convert.ToInt16(txt_key.Text);

            value = Convert.ToInt16(txt_value.Text);

            obj.Add(key, value);

            MessageBox.Show("a key-value pair is added to ordered dictionary");

           

        }

VB:

Private Sub btn_add_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim key As Integer

        Dim value As Integer

        key = Convert.ToInt16(txt_key.Text)

        value = Convert.ToInt16(txt_value.Text)

        obj.Add(key, value)

 

        MessageBox.Show("a key-value pair is added to ordered dictionary")

    End Sub

Clear the ordered dictionary:

In order to remove all the entries from the ordered dictionory, ordered dictionary class provides us a function named as clear(). This function takes no argument as a parameter . As its return type is void so it doesn’t return anything.

To demonstrate make a new window application. Drag one button and two text boxes on from.

Now write the following code on Button click event:

C#:

private void button1_Click_2(object sender, EventArgs e)

        {

            obj.Clear();

            MessageBox.Show("all entries are removed from your ordered dictionary");

        }

VB:

Private Sub button1_Click_2(ByVal sender As Object, ByVal e As EventArgs)

        obj.Clear()

        MessageBox.Show("all entries are removed from your ordered dictionary")

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP ORDERED DICTIONARY APPLICATION";    

        }

VB:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP ORDERED DICTIONARY APPLICATION"

    End Sub

This simple article tells that how we can use, initialize, add and remove from ordered dictionary using OrderedDictionary class in vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Add - remove and check that collection contains null keys using NameValueCollection class - vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net