tags:

views:

74

answers:

5

Hi. I'm fairly new to programming C#, and I have written a program that uses hashtables to store data (in my case, the users name, and if they are "Ready" or "Not Ready". I have 2 tables in total. The first table has the key as the username and the IP address of the client in the value box. the second table has the Ready/Not Ready status (given by a combo box) for the key, and the IP address as the value.

The first table isn't a problem, as I don't want the users name to re-occur. However, in the second table I need the Ready/Not Ready status to re-occur many times. However this does not work as it says there is already a key called "Ready" in the hashtable. Is there nay way to get around this?

+1  A: 

You could use a Dictionary<Status,HashSet<IP>> for the second table. This has the additional advantage that inserting/removing an IP is fast since it's a key into the HashSet.

CodeInChaos
Although for distinguishing a dual status value (Ready/Not Ready) a dictionary actually seems overkill.
0xA3
Since he talked about a combo box and not a checkbox I wasn't sure if there are more than two states. If there are only two states then using two HashSets (one for each status) would definitely be preferred.
CodeInChaos
Yes, there are only 2 states. I basically have 3 values for each user (ip address, Username, Ready/Not Ready State). Is there any way to display the value field of a hashtable as opposed to the key?
Alex Godbehere
A: 

Keys have to be unique. If you tried to access a value by the key how would it know which one you really wanted?

Dismissile
+1  A: 

So, the reason for the second hashtable is to quickly look up who is ready or not, correct?

In that case, consider splitting that up into 2 different collections: one for those who are ready, and one for those who are not.

Most likely, a simple List<T> will be fine here, since you just need to see who is in there, rather than finding a specific one (because if you want to do that, you could just look in the other hashtable). If it's important to have similar lookup properties to the hashtable, you can use a HashSet<T> instead, but it depends on your needs.

Michael Madsen
Also, I assume that by "hashtable" you mean the concept, not necessarily the [HashTable](http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx) class - in most cases, a [Dictionary](http://msdn.microsoft.com/en-us/library/xfhwa508.aspx) is a better choice due to the type safety.
Michael Madsen
A: 

It sounds like Hashtable is probably not the ideal data structure for your problem.

Keys within a hashtable / dictionary must be unique, so no, you can't technically store two entries in a hash table that share the exact same key.

Also, you should probably use a Dictionary<TKey, TValue) instead of the actual Hashtable type, as it has better performance characteristics.

You can simulate what you want by doing something like creating a Dictionary whose value is a set of some kind:

// Map containing two sets of IP addresses: those that are ready, 
// and those that are not ready.
var readyMap = new Dictionary<bool, HashSet<string>>();
readyMap[true] = new HashSet<string>();
readyMap[false] = new HashSet<string>();

// Add an IP address that is ready.
readyMap[true].Add(ipAddress1);

// Add an IP address that is not ready.
readyMap[false].Add(ipAddress2);

However, that might not be the ideal solution. What is the actual problem you are trying to solve?

mtreit
Basically, what i am trying to create, is a solution very similar to the "Ready Up" system in games, which notifies the host if users are ready to start the game. Should I post the code for the server and client programs?
Alex Godbehere
I don't think you need to post your code for the whole programs. There are a number of ways to solve this problem, I think. Another approach you might consider is to have a single set of Not Ready addresses that initially contains everybody. When a machine transitions to 'ready' you simply remove it from the 'not ready' set. When the set is empty, everyone is ready. A HashSet<T> is again a good choice for that.
mtreit
Okay, I have finally figured this out. I have 3 tables. First one is Key: Username Value: IP. Second is Key: IP Value: Username. Third is Key:Username Value: Ready/Not Ready. Then I simply reference htReady.Value (the hashtable). My whole code is here: http://pastebin.com/Z60GEjK8 .The parts you want to be looking at are the start of Class ChatServer, AddUser, RemoveUser and AcceptClient.
Alex Godbehere
A: 

Okay, I have finally figured this out. I have 3 tables. First one is Key: Username Value: IP. Second is Key: IP Value: Username. Third is Key:Username Value: Ready/Not Ready. Then I simply reference htReady.Value (the hashtable). My whole code is here: http://pastebin.com/Z60GEjK8 .The parts you want to be looking at are the start of Class ChatServer, AddUser, RemoveUser and AcceptClient.

I am new to this, so If you could suggest a better way, I'm all ears.

Alex Godbehere