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!