Extending Hashtable - is there a better way? [modified]
-
Hello everyone. Some time ago, I was working with
Hashtables
when I found a limitation: as an example I will use the following:Hashtable countryInfo = new HashTable();
countryInfo.Add("Canada", "Ottawa");
countryInfo.Add("UK", "London");
countryInfo.Add("France", "Paris");
...This is great: given the country France, I can find out that it's capital is Paris:
string capital = countryInfo["France"]
What I can't do is find out is what country is Paris the capital of (without looping through the table). So I created
MultiHashTable
which, instead of holdingkey,value
pairs, holdskey,key,key...
sets. I can now do things like:MultiHashTable countryInfo = new MultiHashTable("Country", "Capital", MultiHashTable.Unkeyed("Language"));
countryInfo.Add("Canada", "Ottawa", "English");
countryInfo.Add("UK", "London", "English");
countryInfo.Add("France", "Paris", "French");(note: the "Language" table is "Unkeyed" because it must hold duplicate entries for Canada and UK). Now I can find out what country has Paris as it's capital:
capital = countryInfo.GetItem("Country", "Capital", "Paris");
and other things that I needed to do. My question is this - did I re-invent the wheel? I keep discovering all kinds of stuff already built into C# and I can't help but think that this sort of thing is already in there somewhere. I won't consider my time wasted if it is - creating
MultiHashTable
was a great learning experience - I just want to know if at some time in the future someone is going to say "well why don't you just useSystem.Collections.SuperCrossRef
like everybody else?"Clive Pottinger Victoria, BC
modified on Wednesday, January 23, 2008 1:42:53 PM
-
Hello everyone. Some time ago, I was working with
Hashtables
when I found a limitation: as an example I will use the following:Hashtable countryInfo = new HashTable();
countryInfo.Add("Canada", "Ottawa");
countryInfo.Add("UK", "London");
countryInfo.Add("France", "Paris");
...This is great: given the country France, I can find out that it's capital is Paris:
string capital = countryInfo["France"]
What I can't do is find out is what country is Paris the capital of (without looping through the table). So I created
MultiHashTable
which, instead of holdingkey,value
pairs, holdskey,key,key...
sets. I can now do things like:MultiHashTable countryInfo = new MultiHashTable("Country", "Capital", MultiHashTable.Unkeyed("Language"));
countryInfo.Add("Canada", "Ottawa", "English");
countryInfo.Add("UK", "London", "English");
countryInfo.Add("France", "Paris", "French");(note: the "Language" table is "Unkeyed" because it must hold duplicate entries for Canada and UK). Now I can find out what country has Paris as it's capital:
capital = countryInfo.GetItem("Country", "Capital", "Paris");
and other things that I needed to do. My question is this - did I re-invent the wheel? I keep discovering all kinds of stuff already built into C# and I can't help but think that this sort of thing is already in there somewhere. I won't consider my time wasted if it is - creating
MultiHashTable
was a great learning experience - I just want to know if at some time in the future someone is going to say "well why don't you just useSystem.Collections.SuperCrossRef
like everybody else?"Clive Pottinger Victoria, BC
modified on Wednesday, January 23, 2008 1:42:53 PM