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 / May 2004

Tip: Looking for answers? Try searching our database.

Confused by the NameValueCollection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
anon - 13 May 2004 10:07 GMT
I need a little clarity in the NameValueCollection.

Within the MSDN .NET 1.1 Framework help is says:

   "This collection is based on the NameObjectCollectionBase class.
However, unlike the
   NameObjectCollectionBase, this class stores multiple string values under
a single key."

What does this REALLY MEAN?

IS THIS CORRECT WAY?
EXAMPLE #1
Index     Key    Value
0            a        "alpha"
1            b        "beta"
2            c        "charlie"
3            c        "clifford"

OR does it mean
EXAMPLE #2
Index     Key    Value
0            a        "alpha"
1            b        "beta"
2            c        "charlie", "clifford"

see how I am confused?
Jakob Christensen - 13 May 2004 10:51 GMT
Hey anon

If you add two or more values for the same key to a NameValueCollection, the value for that key will contain a comma-separated list of the values.  For example

NameValueCollection c = new NameValueCollection()
c.Add("a", "1")
c.Add("a", "2")
Console.WriteLine(c["a"]); // Outputs "1,2

Regards, Jakob
Bob Powell [MVP] - 13 May 2004 12:55 GMT
Example 2.

NameValueCollection nvc=new NameValueCollection();

nvc.Add("A","Alpha");

nvc.Add("B","Beta");

nvc.Add("C","Charlie");

nvc.Add("C","Chumpkin");

for(int n=0; n<nvc.Count; n++)

Console.WriteLine(nvc[n]);

foreach(string s in nvc.Keys)

Console.WriteLine(nvc[s]);

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

> I need a little clarity in the NameValueCollection.
>
[quoted text clipped - 23 lines]
>
> see how I am confused?
Teemu Keiski - 13 May 2004 13:03 GMT
Hi,

see the documentaion for NameValueCollection's Add method. It says:

"If the specified key already exists in the target NameValueCollection
instance, the specified value is added to the existing comma-separated list
of values associated with the same key in the target NameValueCollection
instance."

Signature

Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

> I need a little clarity in the NameValueCollection.
>
[quoted text clipped - 23 lines]
>
> see how I am confused?
anon - 13 May 2004 19:26 GMT
What if the values have a "comma" in them?  How is the comma-separated list
going to know where the next "REAL" value is?

> Hi,
>
[quoted text clipped - 33 lines]
> >
> > see how I am confused?
Bob Powell [MVP] - 13 May 2004 22:07 GMT
They are all the real value. The values share a key.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

> What if the values have a "comma" in them?  How is the comma-separated list
> going to know where the next "REAL" value is?
[quoted text clipped - 37 lines]
> > >
> > > see how I am confused?
Chris R. Timmons - 14 May 2004 05:36 GMT
> What if the values have a "comma" in them?  How is the
> comma-separated list going to know where the next "REAL" value
> is?

Use the GetValues() method:

using System;
using System.Collections.Specialized;

namespace ExampleNamespace
{    
 public class TestForm
 {
   [STAThread]
   public static void Main()
   {
     NameValueCollection nvc = new NameValueCollection();

     nvc.Add("A","Alpha");
     nvc.Add("B","Beta");
     nvc.Add("C","Charlie 1,Charlie sub-1");
     nvc.Add("C","Charlie 2");

     foreach(string key in nvc.Keys)
     {
       Console.WriteLine("Key = {0}", key);

       // A key may point to multiple values.  Process
       // the values individually by using the GetValues method.

       foreach(string value in nvc.GetValues(key))
         Console.WriteLine("  Value = {0}", value);
     }
   }
 }
}

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Svein Terje Gaup - 13 May 2004 22:20 GMT
By the way, the System.Web.HttpRequest.QueryString is a NameValue
collection.

Signature

Sincerely
Svein Terje Gaup

> I need a little clarity in the NameValueCollection.
>
[quoted text clipped - 23 lines]
>
> see how I am confused?

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.