Datastructure

S

Shahid

Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?

Thank you in advance.
Shahid
 
L

Larry Lard

Shahid said:
Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?

Just create a class that encapsulates key1 and key2, and use objects of
that class as the keys.

(This assumes you will always have both key1 and key2 available - if
you want to be able to do something like 'give me all values that have
key1=X and key2=anything', then more work would be needed)
 
O

Otis Mukinfus

Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?

Thank you in advance.
Shahid

A Hashtable doesn't know anything about the key you give it except that it ais a
string, so if you have multiple keys you can concatenate them for a compound key
of sorts.

yourHashtable.ADD(Key1 + Key2, yourObject);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
B

Bruce Wood

Another option is a Hashtable of Hashtables, like this:

Hashtable mainTable = new Hashtable();

// Add myValue under key1, key2
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable == null)
{
subTable = new Hashtable();
mainTable[key1] = subTable;
}
subTable[key2] = myValue;

// Get myValue back, given key1 and key2
myValue = null;
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable != null)
{
myValue = (MyClass)subTable[key2];
}
// At this point, if myValue is null then it was not found; if it is
non-null then it was found.

Of course, you could create your own HashOfHashtables class that
wrapped all of this up in a nice package, including an indexer that
could take two keys and do the required lookup / assignment.

Nonetheless, this a more complicated implementation. You should use the
"concatenate the keys" method if possible, and use this only if you
have needs that the simpler solution doesn't meet (such as Larry's "get
me all the values with key1=='x'" operation).

By the way, if you use Larry's / Otis's "concatenate the keys" method,
be sure to add a delimeter in between the keys, something like this:

myHashTable.Add(key1 + "|" + key2, yourObject);

This is so that you don't run into the problem in which the keys "A"
and "BC" end up colliding with the key pair "AB" and "C". If you don't
use a delimeter (which should be outside the set of valid characters
for the key, if that's possible) then you could get collisions like
this. If you do use a delimeter then the first key would be, say,
"A|BC" and the second "AB|C" and there is no collision.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top