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
 

Using and object initialization of NameValueCollection class in vb and C# .net.

Author: Maliha Atteeq
Download Source Code : 981_article13.zip

In this simple article you will learn tha.t how we can use NameValueCollection class in Vb and c#.net

 NameValueCollection class:

                        This class is used to store multiple string values under a single key. In this class each value is a pair of key and value.

How to initialize the NameValueCollection object:

In order to make the object of NameValueCollection class, this class provides us eight over loaded constructors. Depending on your requirement you can use any of them. I will explain the most used constructors in my article.

First constructor:

The first overloaded constructor among the eight is the blank constructor. This constructor as the name suggests take no argument as a parameter. This constructor makes your name value collection of default size and with no value contained.

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.Collections.Specialized.NameValueCollection obj = new System.Collections.Specialized.NameValueCollection();

            MessageBox.Show("collection is build by using default constructor");

        }

VB:

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

        Dim obj As New System.Collections.Specialized.NameValueCollection()

        MessageBox.Show("collection is build by using default constructor")

    End Sub

Second constructor:

The second constructor takes one argument as a parameter which is the initial capacity of the collection. It takes the argument of type integer and build the collection having that initial capacity.

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

Now write the following code on Button click event:

 

C#:

private void btn_caps_Click(object sender, EventArgs e)

        {

            int caps;

            caps = Convert.ToInt16(txt_caps.Text);

            System.Collections.Specialized.NameValueCollection obj = new System.Collections.Specialized.NameValueCollection(caps);

            MessageBox.Show("collection is build with "+Convert.ToString(caps)+" capacity");

 

 

 }

VB:

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

        Dim caps As Integer

        caps = Convert.ToInt16(txt_caps.Text)

        Dim obj As New System.Collections.Specialized.NameValueCollection(caps)

 

 

        MessageBox.Show("collection is build with " & Convert.ToString(caps) & " capacity")

    End Sub

Third constructor:

The third constructor takes the NameValueCollection object as an argument. This constructor build the collection with the same initial capacity of the specified collection object. It also copies all the entries from the specified collection object to 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 button1_Click_1(object sender, EventArgs e)

        {

            int caps;

            caps = Convert.ToInt16(txt_capacity.Text);

            System.Collections.Specialized.NameValueCollection obj = new System.Collections.Specialized.NameValueCollection(caps);

            System.Collections.Specialized.NameValueCollection ob = new System.Collections.Specialized.NameValueCollection(obj);

            MessageBox.Show("collection is build with the specified object");

        }

 

VB:

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

        Dim caps As Integer

        caps = Convert.ToInt16(txt_capacity.Text)

        Dim obj As New System.Collections.Specialized.NameValueCollection(caps)

        Dim ob As New System.Collections.Specialized.NameValueCollection(obj)

        MessageBox.Show("collection is build with the specified object")

    End Sub     

Fourth constructor:

The fourth constructor takes two argument as a parameter. One is the NameValueCollection object and second is the capacity of type integer.This constructor build the collection with the given capacityof the  specified collection object. It also copies those entries from the specified collection object to your collection which comes under your given capacity.

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

        {

            int capacity;

            int caps;

            caps = Convert.ToInt16(txt_spcaps.Text);

            System.Collections.Specialized.NameValueCollection obj = new System.Collections.Specialized.NameValueCollection(caps);

            capacity = Convert.ToInt16(txt_urcaps.Text);

            System.Collections.Specialized.NameValueCollection ob = new System.Collections.Specialized.NameValueCollection(capacity, obj);

            MessageBox.Show("collection is build with your given capacity i.e "+Convert.ToString(capacity));

        }

 

vB:

 

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

        Dim capacity As Integer

        Dim caps As Integer

        caps = Convert.ToInt16(txt_spcaps.Text)

        Dim obj As New System.Collections.Specialized.NameValueCollection(caps)

        capacity = Convert.ToInt16(txt_urcaps.Text)

        Dim ob As New System.Collections.Specialized.NameValueCollection(capacity, obj)

        MessageBox.Show("collection is build with your given capacity i.e " & Convert.ToString(capacity))

    End Sub

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP NameValueCollection APPLICATION";

           

                      

        }

VB:         

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

        Me.Text = "DEVASP NameValueCollection APPLICATION"

   End Sub

This simple article tells that how we can use different constructors for initializing the object of NameValueCollection class by using “NameValueCollection” class in Vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Removing value on given index and getting type of array list using “array list” class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net