Sign in to follow this  
NickHighIQ

Hashtables (C#)

Recommended Posts

I've a question about Hashtables in C#. I was lead to believe that they acted like a table, i.e. they required two keys to access one value. Can they be used in this manner? Is there something else I need to achieve this? Or do I have to make my own? The reason I need it is for collision routines. The hashtable contains references of collision functions which I would invoke based on the two objects' types: Collision.Collisions[obj1.Type][obj2.Type].Invoke(); Or something similar. Is there a better way of doing this, or am I just using the wrong type of dictionary? Or, assuming the table never changes, should I use a two-dimensional array?

Share this post


Link to post
Share on other sites
you can use function overloading to accomplish that. There's probably better way to handle collision detection between entities if you explained how you are handling the collision boundaries and such.

Share this post


Link to post
Share on other sites
What you're looking for in general is known as multiple dispatch (assuming you want the resolution to be at runtime) -- the method called is based on the runtime types of multiple parameters, as opposed to a single parameter (the "this" parameter).

You can use a 2D array for this, in one form or another, as a crude solution. A single hash table is inappropriate -- it's just an associative container using hash functions for fast lookup.

Share this post


Link to post
Share on other sites
Like jpetrie said, using an hashtable for that may be inappropriate, still, you can use it. All you have to do is concatenate whatever objects you want, and use them as the key for the hashtable.

So if, for instance, you have a chess game, you can concatenate "a" and "1", and supply that as a key to an hashtable with the value "pawn", to represent a pawn positioned at a1.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this