Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > NameValueCollection ...

Reply
Thread Tools

NameValueCollection ...

 
 
shapper
Guest
Posts: n/a
 
      03-05-2007
Hello,

I am having some problems in looping through each item in
NameValueCollection:

1 Dim a As New NameValueCollection
2 a.Add("My String", City.NewYork)
3 a.Add("My String", City.Paris)
4 a.Add("My String", City.London)
5
6 For Each b As KeyValuePair(Of String, City) In a
7 Response.Write(a.Key)
8 Response.Write(a.Value)
9 Next b

I get an error on code line 6:

"Specified cast is not valid."

What am I doing wrong?

Thanks,

Miguel

 
Reply With Quote
 
 
 
 
Konstantinos Pantos
Guest
Posts: n/a
 
      03-05-2007
You're getting an error because NameValueCollection's add method takes two
parameters both of them being of type string.
Thus when you add "a.Add("My String", City.NewYork)" the collection contains
"My String" as key and "NewYork" (the ToString result of the City enymeration).
When you try to cast the second parameter to a City object on line 6 "KeyValuePair(Of
String, City)" it fails because it does not know how to do that.

HTH

Kostas Pantos

> Hello,
>
> I am having some problems in looping through each item in
> NameValueCollection:
>
> 1 Dim a As New NameValueCollection
> 2 a.Add("My String", City.NewYork)
> 3 a.Add("My String", City.Paris)
> 4 a.Add("My String", City.London)
> 5
> 6 For Each b As KeyValuePair(Of String, City) In a
> 7 Response.Write(a.Key)
> 8 Response.Write(a.Value)
> 9 Next b
> I get an error on code line 6:
>
> "Specified cast is not valid."
>
> What am I doing wrong?
>
> Thanks,
>
> Miguel
>



 
Reply With Quote
 
 
 
 
shapper
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 2:55 pm, Konstantinos Pantos <(E-Mail Removed)> wrote:
> You're getting an error because NameValueCollection's add method takes two
> parameters both of them being of type string.
> Thus when you add "a.Add("My String", City.NewYork)" the collection contains
> "My String" as key and "NewYork" (the ToString result of the City enymeration).
> When you try to cast the second parameter to a City object on line 6 "KeyValuePair(Of
> String, City)" it fails because it does not know how to do that.
>
> HTH
>
> Kostas Pantos
>
> > Hello,

>
> > I am having some problems in looping through each item in
> > NameValueCollection:

>
> > 1 Dim a As New NameValueCollection
> > 2 a.Add("My String", City.NewYork)
> > 3 a.Add("My String", City.Paris)
> > 4 a.Add("My String", City.London)
> > 5
> > 6 For Each b As KeyValuePair(Of String, City) In a
> > 7 Response.Write(a.Key)
> > 8 Response.Write(a.Value)
> > 9 Next b
> > I get an error on code line 6:

>
> > "Specified cast is not valid."

>
> > What am I doing wrong?

>
> > Thanks,

>
> > Miguel


Hmmm,

How can I solve this?

I really need to use one of the items as an enumeration type.

I tried Dictionary which was working but it must have unique keys and
I need some repeating keys so I ended you trying NameValueCollection.

Any idea?

Thanks,
Miguel

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      03-05-2007
shapper wrote:
> Hello,
>
> I am having some problems in looping through each item in
> NameValueCollection:
>
> 1 Dim a As New NameValueCollection
> 2 a.Add("My String", City.NewYork)
> 3 a.Add("My String", City.Paris)
> 4 a.Add("My String", City.London)
> 5
> 6 For Each b As KeyValuePair(Of String, City) In a
> 7 Response.Write(a.Key)
> 8 Response.Write(a.Value)
> 9 Next b
>
> I get an error on code line 6:
>
> "Specified cast is not valid."
>
> What am I doing wrong?
>
> Thanks,
>
> Miguel
>


You are creating a non-generic collection and try to use it as a generic
collection.

What you want is a generic dictionary:

Dim a As New Dictionary(Of String, City)

Now the rest of your code should work.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Specialized.NameValueCollection from asp.net to remote object Julia ASP .Net 1 09-23-2004 10:26 AM
Confused by the NameValueCollection anon ASP .Net 5 05-14-2004 04:36 AM
NameValueCollection.GetValues and array especification Alfons Puig ASP .Net 1 08-28-2003 08:59 AM
Converting a QueryString-TYPE string to a NameValueCollection Yuri Vanzine ASP .Net 0 08-18-2003 05:00 PM
Method not found: System.Collections.Specialized.NameValueCollection System.Web.HttpRequest.get_QueryString(). Mark Miller ASP .Net 1 07-18-2003 08:48 PM



Advertisments