473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to modify a Hashtable entry value in-place during enumeration?

Hashtable map = new Hashtable();

map["first"] = 10;
map["second"] = 20;

foreach(DictionaryEntry e in map)
{
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map[key]);
}
For some reason, the code below doesn't compile with some not very helpful
error message.
Please note that I am not trying to modify a key, just a value!

Nov 16 '05 #1
4 17451
Take a look at this article. It's the same issue.

http://www.dotnet247.com/247referenc.../15/75510.aspx

"Vladimir C." <vo**********************@hotmail.com> wrote in message
news:eN*************@tk2msftngp13.phx.gbl...
Hashtable map = new Hashtable();

map["first"] = 10;
map["second"] = 20;

foreach(DictionaryEntry e in map)
{
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map[key]);
}
For some reason, the code below doesn't compile with some not very helpful
error message.
Please note that I am not trying to modify a key, just a value!

Nov 16 '05 #2
Thanks, but I still don't know how to change a value in a Hashtable during
enumaration.
I see no plausible reason why it shouldn't be allowed, as long as I don't
change keys. There are other ways of course, but they all are more expensive
than just changing the value directly through an iterator.

"Rick Stephens" <nu***@nospam.org> wrote in message
news:uT**************@TK2MSFTNGP12.phx.gbl...
Take a look at this article. It's the same issue.

http://www.dotnet247.com/247referenc.../15/75510.aspx

"Vladimir C." <vo**********************@hotmail.com> wrote in message
news:eN*************@tk2msftngp13.phx.gbl...
Hashtable map = new Hashtable();

map["first"] = 10;
map["second"] = 20;

foreach(DictionaryEntry e in map)
{
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map[key]);
}
For some reason, the code below doesn't compile with some not very helpful error message.
Please note that I am not trying to modify a key, just a value!



Nov 16 '05 #3
Vladimir,

We know that so many iterators are readonly.
In fact they really run much faster in readonly/forward only mode e.g.
XPathNavigator, which runs many times faster than XmlDocument. Despite R/O
limitation, because of performance requirements, I would still often prefer
a faster Iterator than a slow randomly modifiable collection.
As you have pointed out, DictionaryEntry objects are the elements of
Hashtable collection.
Modification of their state, whether because of key, or value, modifies the
key-value pair, and necessitates re-computation of Hashtable; invalidating
iterator integrity.

The only way is to store references in Hashtable, e.g.
class number{//Just stores a number, allows manipulation via val variable
public int val;
public number(int val){this.val=val;}
public override string ToString(){return(val.ToString());}
}

//Now we can run the iterator, get the object and modify the contents of
"number" object, instead of Hastable key-value pair
//This would allow indirect modification of logical contens without
disturbing physical key-value pairs
Hashtable map = new Hashtable();
map["first"] =new number(10);
map["second"] = new number(20);
foreach(DictionaryEntry k in map) {
string key=(string) k.Key;
if(map[key].ToString()=="10")//if it is number 10
((number)map[key]).val=100;//change the value to 100
Console.WriteLine("{0}: {1}", key, map[key]);
}
}

Fakher Halim
Software Architect
TPG

"Vladimir C." <vo**********************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Thanks, but I still don't know how to change a value in a Hashtable during
enumaration.
I see no plausible reason why it shouldn't be allowed, as long as I don't
change keys. There are other ways of course, but they all are more expensive than just changing the value directly through an iterator.

"Rick Stephens" <nu***@nospam.org> wrote in message
news:uT**************@TK2MSFTNGP12.phx.gbl...
Take a look at this article. It's the same issue.

http://www.dotnet247.com/247referenc.../15/75510.aspx

"Vladimir C." <vo**********************@hotmail.com> wrote in message
news:eN*************@tk2msftngp13.phx.gbl...
Hashtable map = new Hashtable();

map["first"] = 10;
map["second"] = 20;

foreach(DictionaryEntry e in map)
{
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map[key]);
}
For some reason, the code below doesn't compile with some not very helpful error message.
Please note that I am not trying to modify a key, just a value!


Nov 16 '05 #4
Simple: You forgot to define the key,
so wont compile.

before calling:

Console.WriteLine("{0}: {1}", key, map[key]);

must define the key thus: (which you probably forgot to do

string key;
foreach(DictionaryEntry e in map)
{
key = e.Key;
e.Value = 100;
Console.WriteLine("{0}: {1}", key, map[key]);
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LC's No-Spam Newsreading account | last post by:
I have the following arrangement working under Netscape 3 / Unix, IE6 / Win and Konqueror / Linux, but NOT under Netscape 7 Unix or Mozilla Linux (silently fails) nor under Netscape 4 Unix (fails...
0
by: sienayr | last post by:
Greetings, I have searched hi and lo through the groups and haven't found my problem specifically. I have listed below what I have tried based on what I have found in the groups. Please let me...
5
by: Belee | last post by:
I have created a static class which contains properties. I can assign values to them when the software is running. How can I create default values for these properties when I am coding them, so...
11
by: darrel | last post by:
let's say I have a string that can be any number of items comma delimited: 2, 4, 6, 12, 345 I want to check against those values in an if/then statement: if integer = any value in the...
3
by: Jim Adams | last post by:
I'm counting the frequency of word occurances and would like to return a key/value list sorted descending by frequency. So I need to quickly see if a word (key) is in the list, but later sort...
0
by: dumbo | last post by:
I do not succeed to modify the content of a MaskedTextBox control whose Property "OutputText" is in binding with field of type text of one table in database a MDB. The control has been generated...
6
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below...
1
by: atul123456789 | last post by:
Hi can i get key corresponding to a particular value in hashtable in java. we can get value corresponding to particular key but if we want to get key corresponding to particular value............
2
by: Hvid Hat | last post by:
class Sprite { private Vector2 position; public Vector2 Position { get { return position; } set { position = value; } } ... }
0
by: madhuriks | last post by:
hi, i developed Xml File using DOM..if user enter the values for modifcation it is modifying at the end of Xml file..but i need is what ever value the user enters it should search that value in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.