Visual Basic .NET - NameValueCollection

In VB.NET, NameValueCollection is a class that stores a collection of associated String keys and String values. It is derived from the System.Collections.Specialized.NameValueCollection class and is typically used to store a collection of query string or form data in web applications.

Creating a NameValueCollection object is simple:

Dim nvc As NameValueCollection = New NameValueCollection()

You can add key-value pairs to the NameValueCollection using the Add method:

nvc.Add("key1", "value1")
nvc.Add("key2", "value2")

You can also use the indexer to add key-value pairs:

nvc("key3") = "value3"

To retrieve values from the collection, you can use the indexer with the key:

Dim value As String = nvc("key1")

You can also use the Get method:

Dim value As String = nvc.Get("key1")

To retrieve all keys in the collection, you can use the AllKeys property:

For Each key As String In nvc.AllKeys
    Console.WriteLine(key)
Next

To retrieve all values in the collection, you can use the AllValues property:

For Each value As String In nvc.AllValues
    Console.WriteLine(value)
Next

You can also use the Count property to get the number of key-value pairs in the collection:

Dim count As Integer = nvc.Count

And you can use the Remove method to remove a key-value pair:

nvc.Remove("key1")

Reading values from a Web.config file

Suppose you have a Web.config file with the following appSettings section:

<appSettings >
  <add key="foo" value="123" / >
  <add key="bar" value="abc" / >
</appSettings >

You can read the values of these keys into a NameValueCollection object like this:

Dim appSettings As NameValueCollection = ConfigurationManager.AppSettings
Dim fooValue As String = appSettings("foo")
Dim barValue As String = appSettings("bar")
Console.WriteLine("foo = {0}", fooValue)
Console.WriteLine("bar = {0}", barValue)

Building a query string

Suppose you have a form with some input fields and you want to build a query string from the values of those fields. You can use a NameValueCollection to do this:

Dim queryString As New NameValueCollection()
queryString("name") = "John Mark"
queryString("age") = "30"
queryString("gender") = "male"
Dim url As String = "https://example.com?" & queryString.ToString()
Console.WriteLine(url)
'This will output:
'https://example.com?name=John+Mark&age=30&gender=male

Parsing query string parameters

Suppose you have a URL with some query string parameters and you want to extract those parameters into a NameValueCollection object. You can use the HttpUtility.ParseQueryString method to do this:

Dim url As String = "https://example.com/?name=John+Mark&age=30&gender=male"
Dim queryString As NameValueCollection = HttpUtility.ParseQueryString(New Uri(url).Query)
Dim nameValue As String = queryString("name")
Dim ageValue As Integer = Integer.Parse(queryString("age"))
Dim genderValue As String = queryString("gender")
Console.WriteLine("name = {0}", nameValue)
Console.WriteLine("age = {0}", ageValue)
Console.WriteLine("gender = {0}", genderValue)