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
 

Use – initialize – add - clear and remove a key-value pair from the list dictionary collection using ListDictionary class – vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 991_listDictionary.zip

In this simple article you will learn that how to use, initialize, add, clear and remove a key-value pair from the list dictionary collection using ListDictionary class in vb and C# .net.

 List Dictionary:

List dictionary is a simple implementation of IDictionary using a singly linked list. It is smaller and faster than a Hashtable if the number of elements is 10 or less. This should not be used if performance is important for large numbers of elements. Items in a List Dictionary are not in any guaranteed order; code should not depend on the current order. The List Dictionary is implemented for fast keyed retrieval; the actual internal order of items is implementation-dependent

Initialization:

In order to initialize the object of list dictionary, list dictionary class provides us two overloaded constructor. Based on your requirements, you can select any one of them. I will discuss the most common and used constructor.

Blank constructor:

The first overloaded constructor is the default blank constructor. It takes no argument as a parameter and build the list dictionary with the default size having zero entries.

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

Now write the following code on Button click event:

C#:

private void btn_build_Click(object sender, EventArgs e)

        {

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

            MessageBox.Show("list dictionaey is build using blank constructor");

        }

VB:

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

        Dim obj As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()

        MessageBox.Show("list dictionaey is build using blank constructor")

    End Sub

 

 

Add an entry:

In order to add a key-value pair in list dictionary collection, list dictionary class provides us function named as add(). This function takes two argument as a parameter which are key and value. 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 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 list 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 list dictionary")

    End Sub

Remove all entries:

In order to remove all key-value pairs from the list dictionary collection, list dictionary class provides us  a function named as clear(). This function takes no argument as a parameter. The return type of this function is void as it returns nothing.

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

Now write the following code on Button click event:

C#:

private void btn_clear_Click(object sender, EventArgs e)

        {

            obj.Clear();

            MessageBox.Show("all entries from list dictionary is removed ");

        }

 

VB:

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

        obj.Clear()

        MessageBox.Show("all entries from list dictionary is removed ")

    End Sub

 

Remove an entry:

In order to remove a key-value pair from the list dictionary collection, list dictionary class provides us  a function named as remove(). This function takes one argument as a parameter which is the key. The function removes that key-value pair from list dictionary collection whose key is equal to the given key. The return type of this function is void as it returns nothing.

To demonstrate make a new window application. Drag one button and one textbox 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 list dictionary having 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 list dictionary having key= " + Convert.ToString(key))

    End Sub                                            

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP LIST DICTIONARY APPLICATION";

                    

        }

 

VB:

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

        Me.Text = "DEVASP LIST DICTIONARY APPLICATION"

 

    End Sub

This simple article tells that how to use, initialize, add, clear and remove a key-value pair from the list dictionary collection using ListDictionary class in vb and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Remove key-value pair - check privileges – size and synchronization of the hybrid dictionary collection using HybridDictionary class vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net