Search Amazon:

.NET NameValueCollection DataTypes

A NameValueCollection is similar to a .NET Hash Table in that it can store items in key/value pairs and allows you to retrieve items by specifying either the Key or index. In fact, NameValueCollections use the hash table and hash table algorithms for their underlying data structures.

Unlike a hash table that can store any type of object as its key or value, NameValueCollections can only store string values. However, a NameValueCollection can store multiple strings values for each key. Because NameValueCollections use the hash table algorithms, they are slower performance wise than hash tables.

The following code illustrates using a NameValueCollection. First, however, a little background. Server variables are a collection of server environmental variables. There are a number of variables you can access by retrieving the headers sent by the client to the page URL the client navigated from. In ASP.NET, ServerVariables is a property of the HttpRequest class and contains a name/value collection of all server variables.

You can retrieve a single variable using this VB.NET code:

   'Retrieve a single server variable.
   Dim strVar as string = Request.ServerVariables("ALL_HTTP")

to get all HTTP headers returned by the client. Example:

   HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/*
   HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:en-us 
   HTTP_COOKIE:AspSessionId=4ggox5ynixgfrn45nkn33o45; 
   ASPSESSIONIDQQQGGAIC=IOANAEMAJNLONOAOAFAPIJJG HTTP_HOST:localhost
   HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; COM+ 1.0.2523)

You can retrieve all server variables into a NameValueCollection:

   Imports System.Collections.Specialized

   Dim nvcSrvElements As NameValueCollection = Request.ServerVariables

Now you can retrieve a value or key. Again in VB.NET:

   Imports System.Collections.Specialized

   Dim nvcSrvElements  As NameValueCollection = Request.ServerVariables
   '
   ' Get a comma delimited list of values for a given key or index:
   '
   Response.Write(nvcSrvElements .Get("ALL_HTTP"))
   Response.Write(nvcSrvElements .Get(0))
   ' 
   ' Get a key for a given index:
   '
   Response.Write(nvcSrvElements .GetKey(0))
   ' 
   '  Get the third string value associated with the first key:
   '
   Response.Write(nvcSrvElements .GetValues(0)(2))

A NameValueCollection also has methods to return a collection of all keys, all values, add and remove items and to get an enumerator so you can enumerate through the collection. These methods are similar to those for a standard collection or hash table. See my Hash Table and ArrayList page for details.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms