NameValueCollection ...

Discussion in 'ASP .Net' started by shapper, Mar 5, 2007.

  1. shapper

    shapper Guest

    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
    shapper, Mar 5, 2007
    #1
    1. Advertising

  2. 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
    >
    Konstantinos Pantos, Mar 5, 2007
    #2
    1. Advertising

  3. shapper

    shapper Guest

    On Mar 5, 2:55 pm, Konstantinos Pantos <> 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
    shapper, Mar 5, 2007
    #3
  4. 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
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Mar 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Miller
    Replies:
    1
    Views:
    1,061
    Mark Miller
    Jul 18, 2003
  2. Yuri Vanzine
    Replies:
    0
    Views:
    2,809
    Yuri Vanzine
    Aug 18, 2003
  3. Alfons Puig
    Replies:
    1
    Views:
    1,509
    Shaji
    Aug 28, 2003
  4. anon
    Replies:
    5
    Views:
    683
    Chris R. Timmons
    May 14, 2004
  5. Julia
    Replies:
    1
    Views:
    501
    Girish bharadwaj
    Sep 23, 2004
Loading...

Share This Page