471,603 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

generic reset of values for hashtable

I have a reason to resuse hashtables and I'd like to set the values to null
without having to reload the keys.- > which is what the clear method does.

If I try a foreach construct I get an error that I cannot alter the value.
The documentation for using an enumerator and moveNext also have the same
restriction.

WONT WORK:
private Hashtable hashValueNuller(Hashtable theHash)
{
foreach (DictionaryEntry dObj in theHash)
{
dObj.Value = null;
}
return theHash;
}

Is the only way out of this to use a clear and constructor takling an array
of keys ?

--
Andrew
Jan 27 '06 #1
2 2553
Andrew,
Have you tried to use a foreach over the keys of the Hashtable?

Something like:

static private Hashtable hashValueNuller(Hashtable theHash)
{
object[] keys = new object[theHash.Keys.Count];
theHash.Keys.CopyTo(keys, 0);
foreach (object key in keys)
{
theHash[key] = null;
}
return theHash;
}

Note a copy of the keys is made, as changing a key's value is considered a
change to the Hashtable.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"andrewcw" <an******@acw.com> wrote in message
news:5C**********************************@microsof t.com...
|I have a reason to resuse hashtables and I'd like to set the values to null
| without having to reload the keys.- > which is what the clear method does.
|
| If I try a foreach construct I get an error that I cannot alter the
value.
| The documentation for using an enumerator and moveNext also have the same
| restriction.
|
| WONT WORK:
| private Hashtable hashValueNuller(Hashtable theHash)
| {
| foreach (DictionaryEntry dObj in theHash)
| {
| dObj.Value = null;
| }
| return theHash;
| }
|
| Is the only way out of this to use a clear and constructor takling an
array
| of keys ?
|
| --
| Andrew
Jan 27 '06 #2
Thanks !!! - I dont know hoe long it would have taken me to think of that !
It works perfectly.
--
Andrew
"Jay B. Harlow [MVP - Outlook]" wrote:
Andrew,
Have you tried to use a foreach over the keys of the Hashtable?

Something like:

static private Hashtable hashValueNuller(Hashtable theHash)
{
object[] keys = new object[theHash.Keys.Count];
theHash.Keys.CopyTo(keys, 0);
foreach (object key in keys)
{
theHash[key] = null;
}
return theHash;
}

Note a copy of the keys is made, as changing a key's value is considered a
change to the Hashtable.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"andrewcw" <an******@acw.com> wrote in message
news:5C**********************************@microsof t.com...
|I have a reason to resuse hashtables and I'd like to set the values to null
| without having to reload the keys.- > which is what the clear method does.
|
| If I try a foreach construct I get an error that I cannot alter the
value.
| The documentation for using an enumerator and moveNext also have the same
| restriction.
|
| WONT WORK:
| private Hashtable hashValueNuller(Hashtable theHash)
| {
| foreach (DictionaryEntry dObj in theHash)
| {
| dObj.Value = null;
| }
| return theHash;
| }
|
| Is the only way out of this to use a clear and constructor takling an
array
| of keys ?
|
| --
| Andrew

Jan 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Job Lot | last post: by
8 posts views Thread by SenthilVel | last post: by
5 posts views Thread by Bernie V | last post: by
16 posts views Thread by Giovanni D'Ascola | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

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.