Hello,
I am having some problems in looping through each item in
NameValueCollec tion:
1 Dim a As New NameValueCollec tion
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 3 2151
You're getting an error because NameValueCollec tion'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(O f
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
NameValueCollec tion:
1 Dim a As New NameValueCollec tion
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
On Mar 5, 2:55 pm, Konstantinos Pantos <kpan...@dolnet .grwrote:
You're getting an error because NameValueCollec tion'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(O f
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
NameValueCollec tion:
1 Dim a As New NameValueCollec tion
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 NameValueCollec tion.
Any idea?
Thanks,
Miguel
shapper wrote:
Hello,
I am having some problems in looping through each item in
NameValueCollec tion:
1 Dim a As New NameValueCollec tion
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Walter Zydhek |
last post by:
I am having a problem using the NameValueCollection type.
If I remove one of the items while iterating through an
collection of this type, I end up with an exception.
This exception is:
Collection was modified after the enumerator was
instantiated.
This happens when it attempts to continue through the
enumeration.
|
by: anon |
last post by:
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."
|
by: Nicole_new C# Programmer |
last post by:
I am creating an application where I have 3 listboxes. The 1st holds the
attributes of our products like division, group, etc. The 2nd listbox holds
the values attach to each attribute like division may have the code ED or DC
and group by have ORP or DVS. Now I am suppose to allow user to select one
attribute and list the values associated to each attribute in listbox two.
Then once they selected the values they want to display this...
|
by: davidw |
last post by:
I always use NameValueCollection. But I read an article says the only
differece between Hashtable and NameValueCollection is that
NameValueCollection could accept more than one value with same key? I am not
quite understand this. And I think NameValueCollection's Set is a nice
method. If I use HashTable, how can I update a item in it? Do I have to
check if it exists, and remove and add it again?
Thanks.
|
by: Vikram404 |
last post by:
Hi,
I am getting the following exception from a NameValueCollection. I
dont understand the situation in which this exception can occur and I
am not able to create it in Debug mode.
The NameValueCollection is supposed to ignore the duplication of keys
and append them but here its throwing an exception saying that the Key
already exists. Can anyone shed more light on this.
| |
by: Steve Long |
last post by:
Can anybody help me out with a code sample or other info on how to implement
SyncRoot on a NameValueCollection? Or, is there another .NET class that does
this for me with the same functionality as a NameValueCollection? Basically,
I need mulitple value for any given key and I need to try and make sure it's
tread safe. I can derive from it but it doesn't implement the SyncRoot
member of the NameObjectCollectionBase.
Thanks in advance...
|
by: Yuri Vanzine |
last post by:
Saw this requested awhile ago on Matthew Reynolds's blog here:
http://www.dotnet247.com/247reference/msgs/11/57944.aspx
Problem: often times I deal with querystring-formatted strings w/o the
actual HttpRequest object involved.
Solution: the function accepts such a querystring-type string and outputs a
NameValueCollection object
|
by: Mike Logan |
last post by:
We are trying to serialize the Request.ServerVariables collection
(NameValueCollection) to an XML formatted string, to insert into a database.
The problem we are running into is that most of the example that we find
discuss writing to a file. Does anyone have a simple function to do this?
We would also like to deserialize the string from the database back into a
NameValueCollection.
--
Mike Logan
|
by: Kerr |
last post by:
Hi all,
I've been scouring the internet for help with this problem and every
occurance i've seen reconstructs the problem but no one seems to have a
solution. Hoping that you guys can help me.
I have a vb.net windows forms project that is using a app.config file.
the contents of the app.config is staggered. see a snippet below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
|
by: MrDotNet |
last post by:
Hi
I want pass NameValueCollection as parameter in webmethod.
I try it but that give me error.
Here is Error.
You must implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits from
ICollection.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |