Multiple Keys

S

Shahid Juma

Using NameValueCollection, is this possible:


my.Add ("1", "VALUE");
my.Add ("1", "VALUE");
my.Add ("2", "VALUE");
my.Add ("2", "VALUE");


Also, how do I iterate through it and print the values that have been
stored.


Thanks in advance,
Shahid
 
C

Craig Deelsnyder

Using NameValueCollection, is this possible:


my.Add ("1", "VALUE");
my.Add ("1", "VALUE");
my.Add ("2", "VALUE");
my.Add ("2", "VALUE");


Also, how do I iterate through it and print the values that have been
stored.


Thanks in advance,
Shahid

Yes it is possible, note also that NameValueCollection is what's used by
the QueryString property (or returned from it). If you add a value for
key that's already there, it then concatenates the 2 values with a , in
between (the value is a comma-separated list). There's only 1 key named
"2".

One way of iterating is that there's a Keys property to iterate over (note
this example I have in front of me is vb.net, you'll have to translate to
C#):

Dim currval as String
For Each key As String In my.Keys
currVal = my.Item(key)
'note if key was "2" I'd get "VALUE,VALUE"
Next
 
S

Shahid Juma

Thanks for the reply. Is there anything else that I can do to achieve
the above besides using NameValueConnection?

Thanks again
Shahid
 
G

Guest

NameValueCollection can store key/value pairs by
unique keys. you can 'Add' multi-values to
one key, e.g.
my.Add ("1", "VALUE");
my.Add ("1", "VALUE");
my.Add ("2", "VALUE");
my.Add ("2", "VALUE");

The result is that it concatenates values separated by comma, e.g.
my("1â€) = “VALUE,VALUEâ€. It seems you can use
string.split(",".ToCharArray()) to get an value array. But suppose there is
one value with comma, e.g. "1,000". How do you deal with it?

Elton Wang
(e-mail address removed)
 
C

Craig Deelsnyder

Shahid said:
Thanks for the reply. Is there anything else that I can do to achieve
the above besides using NameValueConnection?

Thanks again
Shahid

What's the end-result you want? Is NVC not what you want for what
happens to the value? Most other collection types will overwrite the
value (or throw an exception) when you do an .Add on the same key more
than once.
 
S

Shahid Juma

This method is perfect. But I am unsure of how to split it up when I
want to display it.
 
S

Shahid Juma

Just a note, I am using C# and not sure how to display what I entered
in the NameValueConnection.
 
C

Craig Deelsnyder

Shahid said:
Just a note, I am using C# and not sure how to display what I entered
in the NameValueConnection.

Are you saying the example I gave? Here I'll try to translate, you'll
have to cleanup any 'mistakes' on my part (I won't be running this thru
a compiler):


String currVal = "";
foreach (String key in my.Keys)
{
currVal = my.Item(key);
//'note if key was "2" I'd get "VALUE,VALUE"
}
 
C

Craig Deelsnyder

Shahid said:
Thank you, that worked. Thanks for the help
Glad to hear it, as I just noticed I used the wrong index
'delimiter'....(key) is vb.net, [key] is c# :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top