Hashtables (C#)

Started by
2 comments, last by xor 16 years, 1 month ago
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?
Nick Wilson - Junior C# Developer | See my crappy site
Advertisement
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.
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.
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.

This topic is closed to new replies.

Advertisement