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
 

Add - remove and check that collection contains null keys using NameValueCollection class - vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 984_nAMEvALUE.zip

In this simple article you will learn that how we can add, remove and check that whether the name value collection contains null value for keys using NameValueCollection class in vb and c#.net.

 Add an element:

In order to add elements in name value collection, namevalue collection class provides us a function named as add(). This function takes two string argument as an parameter, as the name value collection class add the is used to store key-value pair in it so the two string arguments are key and value. The return type of the function 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)

        {

            string key;

            string value;

            key = txt_key.Text;

            value = txt_value.Text;

            obj.Add(key, value);

            MessageBox.Show("the key-value pair is added to the collection");

        }

 

VB:

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

        Dim key As String

        Dim value As String

        key = txt_key.Text

        value = txt_value.Text

        obj.Add(key, value)

        MessageBox.Show("the key-value pair is added to the collection")

    End Sub

Clear the collection:

In order to remove all the entries from your collection, name value collection class provides us a function named as clear(). This function takes no argument as a parameter and its return type is void.

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

        }  

VB:

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

        obj.Clear()

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

    End Sub

 

For null keys:

In order to check that whether the collection contains null values for keys or not, namevalue collection class provides us a method named as HasKey(). This function takes no arguments as a parameter. Its return type is Boolean. It return true if the collection doesn’t contain any null value for key and return false if the collection contains.

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

        {

            bool temp;

            temp=obj.HasKeys();

            if(temp)

            {

                MessageBox.Show("collection contains no null values for keys");

               

            }

            else

            {

                MessageBox.Show("collection contains null values for keys");

            }

        }

VB:

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

        Dim temp As Boolean

        temp = obj.HasKeys()

        If temp Then

 

            MessageBox.Show("collection contains no null values for keys")

        Else

            MessageBox.Show("collection contains null values for keys")

        End If

    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 add, remove and check that null keys are present in collection using NameValueCollection class in vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get value - key - hashCode and type of NameValueCollection class object - vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net