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

V

Vladimir C.

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!
 
V

Vladimir C.

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 said:
Take a look at this article. It's the same issue.

http://www.dotnet247.com/247reference/msgs/15/75510.aspx

Vladimir C. said:
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!
 
F

Fakher Halim

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. said:
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 said:
Take a look at this article. It's the same issue.

http://www.dotnet247.com/247reference/msgs/15/75510.aspx

Vladimir C. said:
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!
 
P

pashute

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
*-----------------------*
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top