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. Hashtable problem...

Hashtable problem...

Scheduled Pinned Locked Moved C#
helpdata-structurescryptographyquestion
7 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
    Chris Richner
    wrote on last edited by
    #1

    Current issue is making me crazy at the moment, there must be something I missed, 'cause it's not working as expected... I like to access a hash table key either by it's name or a class instance. To achieve this I create a new class (HashKey) which internally has a string called _name. When the hashtable calls HashKey.GetHashCode() it returns the HashCode of the internal wrapped string _name. Should be pretty much the same like I would use a literal string value key directly, right? so it must the be same if I add my value by a string key

    HashTable myHashTable = new HashTable();
    myHashTable.Add("keyName", 4);

    or I add my value by a string key wrapped in HashKey class.

    HashKey myHashKey = new HashKey("keyName");
    myHashTable.Add(myHashKey, 4);

    Now I should/wanna be able to read the value by literal string or by the HashKey instance.

    object value = myHashTable["keyName"];
    //
    value = myHashTable[myHashKey];

    This doesn't work, but WHY? Here is the implemenation of HashKey class, which wraps the string and overrides GetHashCode and Equals methods.

    public class HashKey
    {
    private string _name;

    public HashKey(string name)
    {
    	\_name = name;
    }
    public override string ToString()
    {
    	return \_name;
    }
    
    public override int GetHashCode()
    {
    	return  \_name.GetHashCode();
    }
    public override bool Equals(object obj)
    {
    	return \_name.Equals (obj);
    }
    

    }

    L 1 Reply Last reply
    0
    • C Chris Richner

      Current issue is making me crazy at the moment, there must be something I missed, 'cause it's not working as expected... I like to access a hash table key either by it's name or a class instance. To achieve this I create a new class (HashKey) which internally has a string called _name. When the hashtable calls HashKey.GetHashCode() it returns the HashCode of the internal wrapped string _name. Should be pretty much the same like I would use a literal string value key directly, right? so it must the be same if I add my value by a string key

      HashTable myHashTable = new HashTable();
      myHashTable.Add("keyName", 4);

      or I add my value by a string key wrapped in HashKey class.

      HashKey myHashKey = new HashKey("keyName");
      myHashTable.Add(myHashKey, 4);

      Now I should/wanna be able to read the value by literal string or by the HashKey instance.

      object value = myHashTable["keyName"];
      //
      value = myHashTable[myHashKey];

      This doesn't work, but WHY? Here is the implemenation of HashKey class, which wraps the string and overrides GetHashCode and Equals methods.

      public class HashKey
      {
      private string _name;

      public HashKey(string name)
      {
      	\_name = name;
      }
      public override string ToString()
      {
      	return \_name;
      }
      
      public override int GetHashCode()
      {
      	return  \_name.GetHashCode();
      }
      public override bool Equals(object obj)
      {
      	return \_name.Equals (obj);
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I copied your code and stepped through the debugger, and this is the solution, it works:

      	public override bool Equals(object obj)
      	{
      		if (obj is HashKey)
      			return \_name.Equals(((HashKey)obj).\_name);
      		else
      			return \_name.Equals(obj);
      	}
      

      You need to cast the object when necessary and access the _name attribute directly. regards

      C 1 Reply Last reply
      0
      • L Lost User

        I copied your code and stepped through the debugger, and this is the solution, it works:

        	public override bool Equals(object obj)
        	{
        		if (obj is HashKey)
        			return \_name.Equals(((HashKey)obj).\_name);
        		else
        			return \_name.Equals(obj);
        	}
        

        You need to cast the object when necessary and access the _name attribute directly. regards

        C Offline
        C Offline
        Chris Richner
        wrote on last edited by
        #3

        Hi Greeeg, Thanks for your fast reply, just added your code. Now it works to access the key with the HashKey instance

        value = myHashTable[myHashKey];

        but this scenario still doesn't work here

        object value = myHashTable["keyName"];

        Any other changes you made to the code, any ideas? myMsg.BehindDaKeys = "Jerry Maguire";

        L 1 Reply Last reply
        0
        • C Chris Richner

          Hi Greeeg, Thanks for your fast reply, just added your code. Now it works to access the key with the HashKey instance

          value = myHashTable[myHashKey];

          but this scenario still doesn't work here

          object value = myHashTable["keyName"];

          Any other changes you made to the code, any ideas? myMsg.BehindDaKeys = "Jerry Maguire";

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Strange, both work for me

          Hashtable myHashTable = new Hashtable();
          HashKey myHashKey = new HashKey("keyName");
          myHashTable.Add(myHashKey, 4);
          
          value = myHashTable\["keyName"\];
          value = null;
          value = myHashTable\[myHashKey\];
          

          after both calls "value" is 4. I inserted the "value = null" to see whether the second call will change "value" at all. You might want to add another if-clause like else if(obj is String) return _name.Equals((String)obj) into the Equals method. regards

          C 1 Reply Last reply
          0
          • L Lost User

            Strange, both work for me

            Hashtable myHashTable = new Hashtable();
            HashKey myHashKey = new HashKey("keyName");
            myHashTable.Add(myHashKey, 4);
            
            value = myHashTable\["keyName"\];
            value = null;
            value = myHashTable\[myHashKey\];
            

            after both calls "value" is 4. I inserted the "value = null" to see whether the second call will change "value" at all. You might want to add another if-clause like else if(obj is String) return _name.Equals((String)obj) into the Equals method. regards

            C Offline
            C Offline
            Chris Richner
            wrote on last edited by
            #5

            It doesn't work for me. I think it can't work because in case of asking

            value = myHashTable["keyName"];

            string.Equals() is called which is can never returns true when asked about equality with HashKey instance, right? myMsg.BehindDaKeys = "Jerry Maguire";

            L 1 Reply Last reply
            0
            • C Chris Richner

              It doesn't work for me. I think it can't work because in case of asking

              value = myHashTable["keyName"];

              string.Equals() is called which is can never returns true when asked about equality with HashKey instance, right? myMsg.BehindDaKeys = "Jerry Maguire";

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              But you do an object comparison with the _name-string of your hashkey instance, so you compare two strings in case object is a string.

              C 1 Reply Last reply
              0
              • L Lost User

                But you do an object comparison with the _name-string of your hashkey instance, so you compare two strings in case object is a string.

                C Offline
                C Offline
                Chris Richner
                wrote on last edited by
                #7

                Yep, but I guess the problem is that in case of the string hashtable key not the Hashkey.Equals() method is called. Instead it calls the string "keyName".Equals(hashKeyInstance) which returns false. So in case of asking the value with the string key, it works the other way round with calling Equals(). Is this correct? Thank you very much for your efforts. myMsg.BehindDaKeys = "Jerry Maguire";

                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