Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extending Hashtable - is there a better way? [modified]

Extending Hashtable - is there a better way? [modified]

Scheduled Pinned Locked Moved C#
questioncsharphelptutoriallearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    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 holding key,value pairs, holds key,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 use System.Collections.SuperCrossRef like everybody else?"

    Clive Pottinger Victoria, BC

    modified on Wednesday, January 23, 2008 1:42:53 PM

    E 1 Reply Last reply
    0
    • C Clive D Pottinger

      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 holding key,value pairs, holds key,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 use System.Collections.SuperCrossRef like everybody else?"

      Clive Pottinger Victoria, BC

      modified on Wednesday, January 23, 2008 1:42:53 PM

      E Offline
      E Offline
      engsrini
      wrote on last edited by
      #2

      Iam not sure what version you are working on, i think this can be done with Dictionary(.net 2.0), Please take a look at it. Thanks Srini

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups