开发者

C# : Using hashtables to store two of the same value. Is it possible?

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. 开发者_如何学JAVAThe 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?


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.


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.


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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜