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
 

Get - count - set and remove keys from NameValueCollection class object - vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 982_application.zip

In this simple article you will learn that how we can get count, keys of the collection and also how we can set and remove the key-value pair from the NameValue Collection using NameValueCollection cl

 To get all keys:

In order to get all keys of the collection, nameValue collection class provides us a property known as AllKeys. It returns an array of strings containing the keys of your collection.

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

        {

            String[] arr;

            arr = obj.AllKeys;

            int i = 0;

            while (i < arr.Length)

            {

                MessageBox.Show(arr[i]);

                i++;

            }

        }

VB:

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

        Dim arr As [String]()

        arr = obj.AllKeys

        Dim i As Integer = 0

        While i < arr.Length

            MessageBox.Show(arr(i))

            i += 1

        End While

    End Sub

Get count of collection:

In order to get the collection of the collection, nameValue collection class provides us a property known as count. Its return type is integer and it returns the total count of the key-value pair of your collection.

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

        {

            int count;

            count = obj.Count;

            MessageBox.Show(Convert.ToString(count));

        }

VB:

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

        Dim count As Integer

        count = obj.Count

        MessageBox.Show(Convert.ToString(count))

    End Sub

To set the key-value pair:                                                                              

In order to set the key-value pair in your collection, name value collection class provides us a function named as set(). This function takes no argument as a parameter and its return type is void. This function sets your key-value pair in your collection.

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

        {

            string key;

            string value;

            key = txt_key.Text;

            value = txt_val.Text;

            obj.Set(key, value);

            MessageBox.Show("a value is added to your collection");

        }

VB:

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

        Dim key As String

        Dim value As String

        key = txt_key.Text

        value = txt_val.Text                                                                                  

        obj.[Set](key, value)                                                                                 

        MessageBox.Show("a value is added to your collection")

    End Sub

To remove from collection:

In order to remove a specific key from the collection, name value collection class provides us a function named as remove(). This function takes one argument as a parameter and that is the value of the key of type string. 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)

        {

            string key;

            key = txt_remove.Text;

            obj.Remove(key);

            MessageBox.Show("given key is removed fom the collection");

           

           

        }

VB:

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

        Dim key As String

        key = txt_remove.Text

        obj.Remove(key)

 

 

        MessageBox.Show("given key is removed fom the collection")

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP NAMEVALUE COLLECTION APPLICATION";

            obj.Add("abc", "namevalue1");

            obj.Add("xyz", "namevalue2");

            obj.Add("lmn", "collection1");

            obj.Add("opq", "collection2");

            obj.Add("efg", "collection3");

           

        }

VB:

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

        Me.Text = "DEVASP NAMEVALUE COLLECTION APPLICATION"

        obj.Add("abc", "namevalue1")

        obj.Add("xyz", "namevalue2")

        obj.Add("lmn", "collection1")

        obj.Add("opq", "collection2")

        obj.Add("efg", "collection3")

 

 

    End Sub

This simple article tells that how we can get count, keys of the collection and also how we can set and remove the key-value pair from the NameValue Collection using NameValueCollection class in vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using and object initialization of NameValueCollection class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net