Advertisement

Another C# question..

Started by December 18, 2002 05:02 AM
17 comments, last by NotAnAnonymousPoster 21 years, 11 months ago
Okay I''m getting lazy now. I could probably look all this up on the internet. When you override GetHashCode() how do you take a hashtable''s load factor into account? Thanks
"C combines all the power of assembly language with all the ease of use of assembly language"
Actually, I can''t even get my implementation of GetHashCode() to work:


  class Point{	public Point(int x, int y)	{		this.x = x;		this.y = y;	}	public int X { get {return x;} set {x = value;} }	public int Y { get {return y;} set {y = value;} }		public override string ToString()	{		return "(" + x + "," + y + ")";	}	public override int GetHashCode()	{		return x^y;	}		private int x,y;}namespace Hashtables{	using System;	using System.Collections;	/// <summary>	/// Summary description for Class1.	/// </summary>	class Hashtables	{		/// <summary>		/// The main entry point for the application.		/// </summary>		[STAThread]		static void Main(string[] args)		{			Hashtable myHT = new Hashtable();			myHT.Add(new Point(3,5), "a house");			myHT.Add(new Point(1,2), "a church");			myHT.Add(new Point(0,0), "a corner shop");			Point atPoint = new Point(0,0);			Console.WriteLine(atPoint + "=" + myHT[atPoint]);		}	}}  

"C combines all the power of assembly language with all the ease of use of assembly language"
Advertisement
I honestly don''t that much about hashtables but are you not referencing it wrong? there are three "buckets" with values in them and as far as I know [0,0] is not in that collection... you have the objects in the collection [3] [6] [9] plus you have to use a value to write out somthing in an array because it is "boxed" so you have to unbox it by using somthing like ToString() then print that value out.
I don''t understand. Why are the objects in my collection [3], [6] and [9]? I don''t think I understand hash tables.


  static void Main(string[] args){	Hashtable myHT = new Hashtable();	Point atPoint = new Point(0,0);			myHT.Add(new Point(3,5), "a house");	myHT.Add(new Point(1,2), "a church");	myHT.Add(atPoint, "a corner shop");	Console.WriteLine(atPoint + "=" + myHT[atPoint]);}  


That works. So the key to access "a corner shop" needs to be the same as the key used to make the entry in the hash table? How do I fix that?

Thanks!
"C combines all the power of assembly language with all the ease of use of assembly language"
Why not use an array list of objects with xy coordinates and a string identifier?
ArrayList theBuildings = new ArrayList();

and have the buildings class have an x and y and name and referance them like

  ((Building)bildings[i]).name;or ((Building)buildings[i].coord.x (or y);//which would be //a Point  

this is how I did my collision dectection on a little "snake" game I made in C# and GDI+


[edited by - djkno3 on December 18, 2002 10:25:59 AM]
quote: Original post by NotAnAnonymousPoster
When you override GetHashCode() how do you take a hashtable''s load factor into account?

You don''t. The load factor is an internal implementation detail of the HashTable class. It doesn''t concern you when you are implementing GetHashCode().

As for your code - you are required to also implement Equals for a class if you implement GetHashcode(Doesn''t the compiler give a warning about this?). If two objects are equal, the contract for GetHashcode states that they should also return the same hashcode.

If your class doesn''t have a notion of equality(overrides Equals), it simply won''t work as a hashtable key(which is what you are experiencing). Note also that it is a bad idea to have mutable objects as hashtable keys.

Add something like this:


  public override boolean Equals( object obj ){   if( rhs is Point )   {      Point rhs = (Point)obj;      return this.x == rhs.x && this.y == rhs.y;   }   else      return false;}  



For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Advertisement
quote: Original post by NotAnAnonymousPoster

That works. So the key to access "a corner shop" needs to be the same as the key used to make the entry in the hash table? How do I fix that?

It works because the default implementation of Equals() in object checks for reference equality.


For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I was going to point out the ToEquals() overriding also because the book I have on .NET says you have to override it or get a compiler error but I then put his code in a new project in VS .NET 2003 and surprise no compiler error....
Bugger. I''m using Programming C# by O''Reilly and he does actually say to implement Equals(). Thanks!

I found a few articles that referred to an object being mutable. What does it actually mean?

"C combines all the power of assembly language with all the ease of use of assembly language"
Your point should probably be a struct (value type).

Btw- you do know that there is a System.Drawing.Point struct as well as System.Drawing.PointF struct included with the framework?



-SniperBoB-

What if The Matrix was recursive?

This topic is closed to new replies.

Advertisement