2

Half the time I need to find a value based on string, their name, and the other half I need to find a value based on an int, their user ID.

Currently I have two dictionaries to solve this dilemma - one that uses a string as a key and one that uses an int as a key. I was wondering if there is a more efficient way to do this - a way to get a value based on int or string.

    public static Dictionary<int, Player> nPlayers = new Dictionary<int, Player>();
    public static Dictionary<string, Player> sPlayers = new Dictionary<string, Player>();

After scanning the other questions, someone mentioned using a dictionary of dictionaries. If anyone can elaborate on this (if it's the solution I'm looking for), that'd be grand

I don't know much about a tuple, but from what I understand it requires two keys, and what I am looking for takes one or the other.

Would Dictionary<object, Player> do the trick? I have no idea.

Please help me in my narrow-minded coding experience. ;_;

Tako M.
  • 295
  • 1
  • 3
  • 13
  • Wrap it up in a container class :) – leppie May 29 '12 at 17:52
  • two distinct dictionaries is better, but why you keep dictionaries here, can't you have property for User id and name in your player object? – Damith May 29 '12 at 18:12
  • These are worth a look: http://noocyte.wordpress.com/2008/02/18/double-key-dictionary/ and http://www.codeproject.com/Articles/32894/C-Multi-key-Generic-Dictionary – JamieSee May 29 '12 at 18:13
  • Damith: Yes, the Player object contains their user ID and name, but I need an organized way of knowing every single person who is logged on. – Tako M. May 29 '12 at 18:13
  • possible duplicate of [Multi-key dictionaries (of another kind) in C#?](http://stackoverflow.com/questions/1171913/multi-key-dictionaries-of-another-kind-in-c) – nawfal Mar 30 '13 at 18:48

4 Answers4

1

I don't think having a Dictionary<object,Player> is a better idea than having two distinct dictionaries. It will probably take the same amount of memory (since each Player reference will still be stored twice in the unified dictionary), will probably be less clear, and might (conceivably) cause problems with hashcode collisions since your key can be several different types.

I would just keep two dictionary, PlayersByName and PlayersByID, and use them when appropriate.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
1

Do you want to know in the future the original data type that was put in the Dictionary? If not, you have two options:

  1. Stringly typed! - Just use a string as the key and when adding to it, call .ToString() on the integers :)

  2. Be objective - Use an object as the key, that way you can put anything you like inside it.

Based on the 2, I'd recommend the first as you still have some kind of type restrictions in there.

If you do want to know the original data type in the future - your implementation is fine :)

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
1

Your solution (2 dictionaries) is correct one. Dictionary can only be indexed by one stable key. As result you have to keep separate dictionaries to index by different keys.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

As per your comment when user logged in to system you adding them to a dictionary, here you have to add to both dictionaries.

I think you can do this in another way,

   public static List<Player> nPlayers = new List<Player>();

That's only you need, add players when they logged in.

If you want to search by ID, Name or whatever you can query nPlayers and find the Player.

 var playerByID = nPlayers.Where(p= p.ID==givenID).FirstOrDefault();

 var playerByName = nPlayers.Where(p= p.Name==givenName).FirstOrDefault();
Damith
  • 62,401
  • 13
  • 102
  • 153