Click here to Skip to main content
15,929,653 members
Home / Discussions / C#
   

C#

 
GeneralDrawing over controls Pin
Ista16-Apr-05 11:29
Ista16-Apr-05 11:29 
GeneralRe: Drawing over controls Pin
MoustafaS16-Apr-05 12:02
MoustafaS16-Apr-05 12:02 
GeneralRe: Drawing over controls Pin
Ista16-Apr-05 12:27
Ista16-Apr-05 12:27 
GeneralRe: Drawing over controls Pin
MoustafaS16-Apr-05 14:35
MoustafaS16-Apr-05 14:35 
GeneralRe: Drawing over controls Pin
Ista16-Apr-05 16:30
Ista16-Apr-05 16:30 
GeneralRe: Drawing over controls Pin
MoustafaS17-Apr-05 14:32
MoustafaS17-Apr-05 14:32 
GeneralInvalid Cast exception Pin
tantiboh16-Apr-05 9:53
tantiboh16-Apr-05 9:53 
GeneralRe: Invalid Cast exception Pin
Heath Stewart16-Apr-05 10:15
protectorHeath Stewart16-Apr-05 10:15 
foreach in C# compiles to using a collections IEnumerable implementation, where IEnumeration.Current returns an object that (hopefully) casts correctly to whatever type you specify. With a Hashtable, however, foreach compiles to use the Hashtable.GetEnumerator method which returns an IDictionaryEnumerator, where IDictionaryEnumerator.Current returns an IDictionaryEntry, not object. Depending on whether your Game object exists in the key or value of the hashtable, you use IDictionaryEntry.Key or IDictionaryEntry.Value and cast that to your GameRoom type.

You can also use foreach (GameRoom gr in Room.Keys) or foreach (GameRoom gr in Room.Values) depending on whether your object is in the key or value of the hashtable.

I must say you're going about this wrong, however. The reason for using a hashtable is fast lookups, typically on the order of O(1), where your enumeration will be O(n), which is much slower.

For each GameRoom, you should add it and the name with the name as the key and the GameRoom as the value. If you use a case-insentive hash code provider and comparer, you don't have to worry about casing, like in the following example:
Hashtable rooms = new Hashtable(
  new CaseInsensitiveHashCodeProvider(),
  new CaseInsensitiveComparer();
rooms.Add(gr1.Name, gr1);
rooms.Add(gr2.Name, gr2);
Now when you need to get a specific GameRoom, just do something like this:
return rooms["GameRoom1"] as GameRoom;
If you want to see if the GameRoom exists, use Hashtable.Contains("GameRoom1"), which is an O(1) operation since it uses the hash code of the name to find the key immediately if available.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft

[My Articles] [My Blog]
GeneralRe: Invalid Cast exception Pin
Heath Stewart16-Apr-05 10:20
protectorHeath Stewart16-Apr-05 10:20 
GeneralRe: Invalid Cast exception Pin
tantiboh16-Apr-05 13:59
tantiboh16-Apr-05 13:59 
GeneralCrystal Report Speed Pin
Kyaw Soe Khaing16-Apr-05 9:20
Kyaw Soe Khaing16-Apr-05 9:20 
GeneralDynamic Assemblies Pin
Pepperman500016-Apr-05 4:45
Pepperman500016-Apr-05 4:45 
GeneralRe: Dynamic Assemblies Pin
Heath Stewart16-Apr-05 8:11
protectorHeath Stewart16-Apr-05 8:11 
GeneralRe: Dynamic Assemblies Pin
Pepperman500016-Apr-05 8:33
Pepperman500016-Apr-05 8:33 
GeneralRe: Dynamic Assemblies Pin
Heath Stewart16-Apr-05 8:43
protectorHeath Stewart16-Apr-05 8:43 
GeneralRe: Dynamic Assemblies Pin
Pepperman500016-Apr-05 8:50
Pepperman500016-Apr-05 8:50 
GeneralManaging access in nested classes Pin
matthias s.16-Apr-05 4:17
matthias s.16-Apr-05 4:17 
GeneralRe: Managing access in nested classes Pin
leppie16-Apr-05 5:53
leppie16-Apr-05 5:53 
GeneralRe: Managing access in nested classes Pin
Phil Harding16-Apr-05 14:54
Phil Harding16-Apr-05 14:54 
GeneralCross-Frame access Pin
Anonymous16-Apr-05 4:07
Anonymous16-Apr-05 4:07 
GeneralRe: Cross-Frame access Pin
Heath Stewart16-Apr-05 8:46
protectorHeath Stewart16-Apr-05 8:46 
GeneralRe: Cross-Frame access Pin
Anonymous16-Apr-05 9:33
Anonymous16-Apr-05 9:33 
GeneralRe: Cross-Frame access Pin
Heath Stewart16-Apr-05 9:37
protectorHeath Stewart16-Apr-05 9:37 
GeneralRe: Cross-Frame access Pin
Anonymous16-Apr-05 10:05
Anonymous16-Apr-05 10:05 
GeneralRe: Cross-Frame access Pin
Heath Stewart16-Apr-05 10:32
protectorHeath Stewart16-Apr-05 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.