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
 

Insert - remove and get type of ordered dictionary collection using OrderedDictionary class vb.net - C#.net.

Author: Maliha Atteeq
Download Source Code : 987_orderedDictionary3.zip

In this simple article you will learn that how to insert, remove and get type of ordered dictionary collection using OrderedDictionary class in vb and C# .net.

 Insert a key-value pair:

In order to insert a key-value pair in orderedDictionary collection, ordered dictionary class provides us a function named as insert(). This function takes three arguments as a parameter. The first parameter is the index at which you want to insert a key-value pair, second is the key and third is the value. This function returns nothing as 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_insert_Click(object sender, EventArgs e)

        {

            int index;

            int key;

            int value;

            index = Convert.ToInt16(txt_index.Text);

            key = Convert.ToInt16(txt_key.Text);

            value = Convert.ToInt16(txt_value.Text);

            if (index > 0 && index < obj.Count)

            {

                obj.Insert(index, key, value);

                MessageBox.Show("a key-value pair is insert at index " + Convert.ToString(index));

            }

            else

            {

                MessageBox.Show("Invalid index");

            }

        }

    }

VB:

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

        Dim index As Integer

        Dim key As Integer

        Dim value As Integer

        index = Convert.ToInt16(txt_index.Text)

        key = Convert.ToInt16(txt_key.Text)

        value = Convert.ToInt16(txt_value.Text)

        If index > 0 AndAlso index < obj.Count Then

            obj.Insert(index, key, value)

            MessageBox.Show("a key-value pair is insert at index " & Convert.ToString(index))

        Else

            MessageBox.Show("Invalid index")

        End If

    End Sub

Remove a key-value pair:

In order to remove a key-value pair from the ordered dictionary collection, ordered dictionary collection class provides us a function named as remove(). This function takes one argument as a parameter which is the key and removes that pair whose key is equal to the specified key. The return type of this function is void so it doesn’t return anything.

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 btn_remove_Click(object sender, EventArgs e)

        {

            int key;

            key = Convert.ToInt16(txt_keey.Text);

            obj.Remove(key);

            MessageBox.Show("a key-value pair is removed from collection haning key= "+Convert.ToString(key));

        }

VB:

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

        Dim key As Integer

        key = Convert.ToInt16(txt_keey.Text)

        obj.Remove(key)

        MessageBox.Show("a key-value pair is removed from collection haning key= " & Convert.ToString(key))

    End Sub

Remove a pair from given index:

In order to remove a key-value pair at a specific index from the ordered dictionary collection, ordered dictionary collection class provides us a function named as removeAt(). This function takes one integer argument as a parameter which is the index and removes that key-value pair whose index is equal to the specified index. The return type of this function is void so it doesn’t return anything.

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 btn_remAt_Click(object sender, EventArgs e)

        {

            int index;

            index = Convert.ToInt16(txt_ind.Text);

            if (index > 0 && index < obj.Count)

            {

                obj.RemoveAt(index);

                MessageBox.Show("a pair is removed from collection having index= "+Convert.ToString(index));

            }

            else

            {

                MessageBox.Show("invalid index");

            }

        }

 

VB:

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

        Dim index As Integer

        index = Convert.ToInt16(txt_ind.Text)

        If index > 0 AndAlso index < obj.Count Then

            obj.RemoveAt(index)

            MessageBox.Show("a pair is removed from collection having index= " & Convert.ToString(index))

        Else

            MessageBox.Show("invalid index")

        End If

    End Sub

 

Get type:

In order to get type of  your collection, ordered dictionary class provides us a function named as getType(). This function takes no argument as a parameter. Its return type is system.type as it returns the object of class type.

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.Type ob;

            ob=obj.GetType();

            MessageBox.Show("type of your collection is "+Convert.ToString(ob));

           

        }

VB:

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

        Dim ob As System.Type

        ob = obj.[GetType]()

 

        MessageBox.Show("type of your collection is " & Convert.ToString(ob))

    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";

            obj.Add(1, 10);

            obj.Add(2, 20);

            obj.Add(3, 30);

            obj.Add(4, 40);

            obj.Add(5, 50);

          

        }

VB:

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

        Me.Text = "DEVASP ORDERED DICTIONARY APPLICATION"

        obj.Add(1, 10)

        obj.Add(2, 20)

        obj.Add(3, 30)

        obj.Add(4, 40)

        obj.Add(5, 50)

    End Sub

This simple article tells that how to insert, remove and get type of ordered dictionary collection using OrderedDictionary class in vb and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get count - privileges, - read-only copy and find key in ordered dictionary using OrderedDictionary class VB.NET - C# .NET.

Disclaimer - Privacy
© 2002-2017 DevASP.net