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 hybrid dictionary collection using HybridDictionary class vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 988_hybridDictionary1.zip

In this simple article you will learn that how to use, initialize, add and clear the hybrid dictionary collection using HybridDictionary class in vb and C# .net.

 Hybrid dictionary:

The Hybrid Dictionary – emphasis on Hybrid – will internally use a ListDictionary while the count is less than or equal to 10 and only when the list becomes larger does it convert to using a Hashtable. The HybridDictionary is best used in situations where some lists are small and others are very large or you just aren’t sure how big a list might eventually grow. The generic equivalent of  hybrid dictionary collection is the Dictionary<>.  

Initialization:

In order to initialize the object of hybrid dictionary, hybrid dictionary class provides us four overloaded  constructors. Based on our requirements we can select any of them to build the hybrid dictionary collection. I will discuss the most common and used constructors in my article.

First constructor:

The first overloaded constructor is blank constructor. It takes no argument as a parameter and build the hybrid dictionary collection 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 btn_build_Click(object sender, EventArgs e)

        {

            System.Collections.Specialized.HybridDictionary ob = new System.Collections.Specialized.HybridDictionary();

            MessageBox.Show("a hybrid dictionary collection is build by using blank constructor");

        }

VB:

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

        Dim ob As New System.Collections.Specialized.HybridDictionary()

        MessageBox.Show("a hybrid dictionary collection is build by using blank constructor")

    End Sub

Second constructor:

The second overloaded constructor takes one integer argument as a parameter which is the initial size of the hybrid dictionary collection. This constructor build the collection with the specified 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 btn_buildsize_Click(object sender, EventArgs e)

        {

            int size;

            size = Convert.ToInt16(txt_size.Text);

            System.Collections.Specialized.HybridDictionary ob = new System.Collections.Specialized.HybridDictionary(size);

            MessageBox.Show("A hybrid dictionary collection is build with initial size "+Convert.ToString(size));

           

        }

VB:

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

        Dim size As Integer

        size = Convert.ToInt16(txt_size.Text)

        Dim ob As New System.Collections.Specialized.HybridDictionary(size)

 

        MessageBox.Show("A hybrid dictionary collection is build with initial size " & Convert.ToString(size))

    End Sub

Add an element:

In order to add a key-value pair in the hybrid dictionary collection, hybrid dictionary class provides us a function named as add(). This function takes two argument as a parameter, one is the key and second is the value. This function adds this entry to your hybrid dictionary collection. 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_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 your hybrid dictionary collection");

        }

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 your hybrid dictionary collection")

    End Sub

Clear the collection:

in order to remove all the key-value pairs from the hybrid dictionary collection, hybrid dictionary collection class provides us a function named as clear(). This function takes no argument as a parameter. Its return type is void so it doesn’t return anything.

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 your collection is removed");

        }

 

VB:

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

        obj.Clear()

        MessageBox.Show("all entries from your collection is removed")

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP HYBRID DICTIONARY APPLICATION";

        }

VB:

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

        Me.Text = "DEVASP HYBRID DICTIONARY APPLICATION"

    End Sub

This simple article tells that how to use, initialize, add and clear the hybrid dictionary collection using HybridDictionary class in vb and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Insert - remove and get type of ordered dictionary collection using OrderedDictionary class vb.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net