Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

How to convert NameValueCollection to string?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
walter - 31 Aug 2005 13:58 GMT
Here is my question: Request.QueryString on asp.net is typed as
NameValueCollection. But there is strange behavoir-- I can simply use
Request.QueryString , or public function ToString() to return a
well-formatted string, ie. param1=1&param2=2..., but when use the same code
on other NameValueCollection object, it only return the class name.

I'm wondering 1. how .NET framework achieve this? by adding logic to check
if it's QueryString object ? 2. Is there a simple way,other than looping thru
kind of labor work,  to convert a NameValueCollection object to a
well-formatted string ?

Thanks.
Cowboy (Gregory A. Beamer) - MVP - 31 Aug 2005 14:39 GMT
The QueryString is cast out as an HttpValueCollection (from the Get on
HttpRequest):

     if (this._queryString == null)
     {
           this._queryString = new HttpValueCollection();
           if (this._wr != null)
           {
                 this.FillInQueryStringCollection();
           }
           this._queryString.MakeReadOnly();
     }

ToString() is overloaded in the HttpValueCollection object to unwind the
name value collection:

internal virtual string ToString(bool urlencoded)
{
     StringBuilder builder1 = new StringBuilder();
     int num1 = this.Count;
     for (int num2 = 0; num2 < num1; num2++)
     {
           string text3;
           string text1 = this.GetKey(num2);
           if (urlencoded)
           {
                 text1 = HttpUtility.UrlEncodeUnicode(text1);
           }
           string text2 = ((text1 != null) && (text1.Length > 0)) ? (text1
+ "=") : "";
           ArrayList list1 = (ArrayList) base.BaseGet(num2);
           int num3 = (list1 != null) ? list1.Count : 0;
           if (num2 > 0)
           {
                 builder1.Append('&');
           }
           if (num3 == 1)
           {
                 builder1.Append(text2);
                 text3 = (string) list1[0];
                 if (urlencoded)
                 {
                       text3 = HttpUtility.UrlEncodeUnicode(text3);
                 }
                 builder1.Append(text3);
           }
           else if (num3 == 0)
           {
                 builder1.Append(text2);
           }
           else
           {
                 for (int num4 = 0; num4 < num3; num4++)
                 {
                       if (num4 > 0)
                       {
                             builder1.Append('&');
                       }
                       builder1.Append(text2);
                       text3 = (string) list1[num4];
                       if (urlencoded)
                       {
                             text3 = HttpUtility.UrlEncodeUnicode(text3);
                       }
                       builder1.Append(text3);
                 }
           }
     }
     return builder1.ToString();
}

If you want other Name/Value collections to be the same way, you can inherit
and create your own overload (the code above should be a good start).

NOTE: Used Lutz Roeder's Reflector with the File Dissasembler for the code.

Signature

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

> Here is my question: Request.QueryString on asp.net is typed as
> NameValueCollection. But there is strange behavoir-- I can simply use
[quoted text clipped - 8 lines]
>
> Thanks.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.