hashtable

V

Vikas Kumar

Hashtable 7/7/2006 2:37 AM
I have this Hasttable windowList

Hashtable windowList = new Hashtable();

To this Hashtable I add a form with a string as key in the following manner.
The string is decided at runtime.

FormX frm = new FormX();
frm.Show();
windowList.add("Form1",frm);

Now I want to close all the Forms which i have added to this hashtable. How
can i do this if i don't know the keys ( coz they were added at runtime )
 
T

Tom Spink

Vikas said:
Hashtable 7/7/2006 2:37 AM
I have this Hasttable windowList

Hashtable windowList = new Hashtable();

To this Hashtable I add a form with a string as key in the following
manner. The string is decided at runtime.

FormX frm = new FormX();
frm.Show();
windowList.add("Form1",frm);

Now I want to close all the Forms which i have added to this hashtable.
How can i do this if i don't know the keys ( coz they were added at
runtime )

Hi Vikas,

You can enumerate the keys, and close your windows like this:

///
foreach ( string key in windowList.Keys )
((Form) windowList[key]).Close();
///
 
M

Mythran

Greg Young said:
Hashtable is enumerable or you could use the Keys property (but the
enumerator will be faster)

foreach(DictionaryEntry entry windowList) {
((Form) entry.Value)
}

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

I agree that it may be faster to use the enumerator...but depending on what
he is doing (closing windows) wouldn't it be safer and possibly more
reliable to enumerate through the keys so the windows can be removed from
the collection once closed? (Otherwise he/she/it would be attempting to
remove items from the collection during the enumeration, which..umm, yeah,
is kinda not right?)

Thanks,
Mythran
 
G

Greg Young

Not really .. there are many ways to deal with having to remove things
during an enumeration. One of the easiest to to work off of a clone which
for small tables is also very cheap to build. There is also the question
which needs to be asked of if you take a form out to close it and you fail,
what do you do? Since this is closing all windows I would imagine that you
have three possible cases, either barf out an error committing suicide, barf
out an error and try to continue (likely in some sort of corrupted state),
or just swallow the error and remove it anyways (atleast you are in a good
state). My thoughts are that most people will either commit suicide or
swallow the error and that relatively few will take the middle road.

If you reflector the keys property you will see why I don't like the idea of
using it much (especially its enumerator).

Cheers,

Greg
 
B

Bruce Wood

Mythran said:
I agree that it may be faster to use the enumerator...but depending on what
he is doing (closing windows) wouldn't it be safer and possibly more
reliable to enumerate through the keys so the windows can be removed from
the collection once closed? (Otherwise he/she/it would be attempting to
remove items from the collection during the enumeration, which..umm, yeah,
is kinda not right?)

You would remove items from the collection as you go only if you're
acting selectively on the items in the table. If you're closing every
single window in the collection, you would just close them in a foreach
loop and then nuke the contents of the entire collection in one go
afterward. I think that this is the cleanest solution:

foreach (Form f in windowList.Values)
{
f.Close();
}
windowList.Clear();

Since the f.Close() does not remove the form from the hashtable, the
foreach will still work.
 

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