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. 7 14666
with a hashtable you can set an existing value using its indexer:
myTable["mykey"] = newValue;
--
cody
Freeware Tools, Games and Humour http://www.deutronium.de.vu || http://www.deutronium.tk
"davidw" <da****@affinisys.com> schrieb im Newsbeitrag
news:eC**************@TK2MSFTNGP10.phx.gbl... 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.
Hi,
The difference between Hashtable and NameValueCollection are the following,
1) As you have mentioned in your post, the NameValueCollection can contain more than one values for a particular key whereas Hashtable can contain only one value for a particular key.
2) The Hashtable collection is much more faster than NameValueCollection, since it uses the Hashtable algorithm which is faster for retrieves.
For the other part of your question about how to update an item in the hashtable,
You can use the Item property to update the value of the key. But you have to note that, if the specified key already exists in the Hashtable, the value is replaced; otherwise, a new element is created.
I guess it is good practice to check whether the key exists in the collection using Contains or ContainsKey method before you try to update.
I hope this helps...
Regards,
Madhu
Microsoft C# MVP | MCSD.NET
"davidw" wrote: 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.
It's also worth mentioning that a NameValueCollection can only contain
strings and has this weird interface where it returns multiple values for
one key as a comma separated string.
"Madhu[C#-MVP]" <Ma*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com... Hi,
The difference between Hashtable and NameValueCollection are the
following, 1) As you have mentioned in your post, the NameValueCollection can contain
more than one values for a particular key whereas Hashtable can contain only
one value for a particular key. 2) The Hashtable collection is much more faster than NameValueCollection,
since it uses the Hashtable algorithm which is faster for retrieves. For the other part of your question about how to update an item in the
hashtable, You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the Hashtable, the
value is replaced; otherwise, a new element is created. I guess it is good practice to check whether the key exists in the
collection using Contains or ContainsKey method before you try to update. I hope this helps...
Regards, Madhu
Microsoft C# MVP | MCSD.NET
"davidw" wrote:
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.
> You can use the Item property to update the value of the key. But you have
to note that, if the specified key already exists in the > Hashtable, the
value is replaced; otherwise, a new element is created.
This is not exactly true. If capacity of hash table is set, simple item[key]
= will add element only when there is free slot. Otherwise assignment will
be "ignored" and you have to use hashTable.Add method to add new value.
HTH
Alex
davidw <da****@affinisys.com> wrote: 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.
Well, it's not quite the only difference. Hashtables can take any
object as the key and value; NameValueCollections only work with
strings. Essentially NameValueCollection has a list of strings for each
key; a Hashtable only has a single value for each key.
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?
No, you can just do
myHashTable[key] = value;
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
AlexS <sa***********@SPAMsympaticoPLEASE.ca> wrote: You can use the Item property to update the value of the key. But you have to note that, if the specified key already exists in the > Hashtable, the value is replaced; otherwise, a new element is created.
This is not exactly true. If capacity of hash table is set, simple item[key] = will add element only when there is free slot. Otherwise assignment will be "ignored" and you have to use hashTable.Add method to add new value.
Could you give an example of that? It seems unlikely to me. Here's a
sample which suggests otherwise:
using System;
using System.Collections;
class Test
{
static void Main()
{
Hashtable table = new Hashtable(5);
for (int i=0; i < 10000; i++)
{
table[i] = i;
}
Console.WriteLine (table.Count);
}
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
capacity will be updated when the load factor of the hashtable is reached.
assignment won't be ignored.
--
Regards,
Hin
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:u9****************@TK2MSFTNGP10.phx.gbl... You can use the Item property to update the value of the key. But you have to note that, if the specified key already exists in the > Hashtable, the value is replaced; otherwise, a new element is created.
This is not exactly true. If capacity of hash table is set, simple item[key] = will add element only when there is free slot. Otherwise assignment will be "ignored" and you have to use hashTable.Add method to add new value.
HTH Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: oliver.wulff |
last post by:
Does there exist a class similar to a hashtable which provides a key-value
pair collection. The key and the value has to be a string?
|
by: John Grandy |
last post by:
What are good rules of thumb to use when deciding between a
NameValueCollection, an ArrayList, or a Hashtable ?
|
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...
|
by: ari |
last post by:
hey all,
i was wondering what the difference is between obtaining a user name like
Request.ServerVariables("Auth_User").Split("\")(1))
and
User.Identity.Name
|
by: Samuel R. Neff |
last post by:
What's the advantage of inheriting from CollectionBase as opposed to
just implementing IList? It seems that it saves you from having to
implement a few properties (Clear, CopyTo, Count,...
|
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...
|
by: Rich |
last post by:
Yes, I need to store some values in an array type collection object that can
hold 3 or more parameters per index. I have looked at the collection object,
hashtable object and would prefer not to...
|
by: SemSem |
last post by:
i want to know waht is an index and how we use it with a simple example
including the main of the program .
thanx
--
Islam Khalil,
|
by: Water Cooler v2 |
last post by:
I can't believe I've stumbled on a simple problem such as this. After
all these years, that too.
Anyway, it goes that I just realized that I could not iterate through
my hashtable and print out...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |