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 value - key - hashCode and type of NameValueCollection class object - vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 983_NameValue3.zip

In this simple article you will learn that how we can get values, keys, hashcode and type of the NameValue Collection using NameValueCollection class in vb and c#.net.

  To get the value:

In order to get the value from the collection, NameValueCollection class provides us a function named as get(). This function has two overloaded methods, based on your requirements you can use any of them.

Its first overloaded function takes one integer argument as a parameter which is the index of your collection. Its return type is string as it returns the value of that specific index.

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

        {

            int index;

            string temp;

            index = Convert.ToInt16(txt_index.Text);

            temp = obj.Get(index);

            MessageBox.Show("Value on given index is " + temp);

 

        }

 

VB:

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

        Dim index As Integer

        Dim temp As String

        index = Convert.ToInt16(txt_index.Text)

        temp = obj.[Get](index)

 

        MessageBox.Show("Value on given index is " & temp)

    End Sub

The second overloaded function takes one string argument as a parameter which is the key value. Its return type is string as it returns the value of that specific index.

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

        {

            string key;

            string temp;

            key = txt_key.Text;

            temp = obj.Get(key);

            MessageBox.Show("value of your given key is"+temp);

        }

 

VB:

 

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

        Dim key As String

        Dim temp As String

        key = txt_key.Text

        temp = obj.[Get](key)

        MessageBox.Show("value of your given key is" & temp)

    End Sub       

To getKey:

In order to get key of your specified index, NameValueCollection class provides us a function named as GetKey(). This function takes an integer argument as a parameter which is the index. Its return type is string as it returns the key of the given index.

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

        {

            int index;

            string temp;

            index =Convert.ToInt16( txt_getkey.Text);

            temp=obj.GetKey(index);

            MessageBox.Show("key of your given index is"+temp);

        }

 

VB:

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

        Dim index As Integer

        Dim temp As String

        index = Convert.ToInt16(txt_getkey.Text)

        temp = obj.GetKey(index)

 

        MessageBox.Show("key of your given index is" & temp)

    End Sub

To get type:

In order to get type of your collection, nameValue collection class provides us a function named as getType(). This function takes no argument as a parameter. Its return type is of system.type which means it returns an object of type class.

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

        {

            System.Type ob;

            ob=obj.GetType();

            MessageBox.Show("type of your collection is "+Convert.ToString(obj));

        }

 

VB:

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

        Dim ob As System.Type

        ob = obj.[GetType]()

        MessageBox.Show("type of your collection is " & Convert.ToString(obj))

    End Sub

 

To get hashCode:

In order to get the hash code of the collection, NameValueCollection class provides us a function named as getHashCode(). This function takes no argument as a parameter and its return type is integer. It returns the hash code.         

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

        {

            int code;

            code=obj.GetHashCode();

            MessageBox.Show("hash code is"+Convert.ToString(code));

        }

 

VB:

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

        Dim code As Integer

        code = obj.GetHashCode()

        MessageBox.Show("hash code is" & Convert.ToString(code))

    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 can get values, keys, hashcode and type of  the NameValue Collection using NameValueCollection class in vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get - count - set and remove keys from NameValueCollection class object - vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net