• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

HOW DO I RETURN A KEY FROM A HASHTABLE/SORTEDLIST

Hello, experts,
I need to return a key from a Hashtable (I presume the same code will work for SortedList)

I know how to return a value, thus:

//this is my hashtable; assume that the values are strings and keys are integers
Hashtable hs = new Hashtable();
hs.Add(key1, value1);
hs.Add(key2, value2);
......

//to return a value (say value2) i write
string temp = (string)hs[key2];

My problem:
How do I return key2 //without using index or looping through the items?

I know I can't write
int temp2 = (int)hs[value2];

The only relevant samples I saw in the VS documentation made use of indexes or loops through the keys. There are 300 or so items in my hashtable, and looping through them each time the user needs just a key doesn't sound efficient to me.

Any help is already being appreciated;)

Dave
0
innovator2
Asked:
innovator2
  • 4
  • 2
  • 2
  • +2
1 Solution
 
SijinCommented:
There is no straightforward way of doing it.

How about using two Hashtables?
one for key->value another for value->key although it is highly inefficient, i can't think of any other way you would be able to do it other than to write your own bi-directional hashtable, which would be rally nice if you are willing to put in the effort.
0
 
innovator2Author Commented:
thank you.
I thought of that but I felt there might be a better way.
Really appreciate it.

Dave
0
 
zeparCommented:
Just add the values as keys to your hashTable during your initialization. Then your hashTable will work just the way you want it to.

//this is my hashtable; assume that the values are strings and keys are integers
Hashtable hs = new Hashtable();
hs.Add(key1, value1);
hs.Add(value1, key1);
hs.Add(key2, value2);
hs.Add(value2, key2);

You don't need two tables.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
innovator2Author Commented:
thanks, zepar.
the trouble is, values are not single items
i.e say key1 = 3, value1 might be "I am value to key 3"
I tried what you said but the system did not recognize the values used as keys.
I'll keep trying...
thanks all the same.

Dave
0
 
zeparCommented:
Your response didn't make sense to me. "I am value to key 3" is a string. Both your keys and values are just objects. HashTables use objects for keys and values.

Here is complete working example that does what you want. Of course you don't have to use literals like I did here.

using System;
using System.Collections;

namespace ConsoleApplication2
{
     /// <summary>
     /// Summary description for Class1.
     /// </summary>
     class Class1
     {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main(string[] args)
          {
               //
               // TODO: Add code to start application here
               //
               //this is my hashtable; assume that the values are strings and keys are integers
               Hashtable hs = new Hashtable();
               hs.Add(1, "value for 1");
               hs.Add(2, "value for 2");
               hs.Add("value for 1", 1);
               hs.Add("value for 2", 2);

               //to return a value (say value1) i write
               System.Console.WriteLine("Value for Key 1="+hs[1]);
               System.Console.WriteLine("Key for Value 1="+hs["value for 1"]);
          }
     }
}


Here is the output.


Value for Key 1=value for 1
Key for Value 1=1
Press any key to continue

0
 
jlachCommented:
umm, why do this? why refernce something by the value..and not by the key? And why would you only need the key, hashtables are to hold information that can be read later.. hence assinging a key, and then grabbing the value at a later point. but if theres no key->value to add, why use a hashtable?
0
 
innovator2Author Commented:
thanks, everyone,
for taking the time to help solve this problem.
To jlach,
I needed a hashtable for what I wanted to do, because it is a value-key pair operation. However, I wanted to return a key, and not the value, in order to perform some other operations.
My program runs like this:
hashtable1, hashtable2, ....hashtablex
-user types in a string value (serves as key)
-program looks in each hashtable to find which of them contains that key, and then return the corresponding value.

In any case, I have figured out a way to solve it, and it works. I used the ContainsKey and the GetKey methods.
e.g. if(hastable1.ContainsKey(theKey))
string result = hashtable1[theKey];
else if(hastable2.ContainsKey(theKey))
string result = hashtable2[theKey];
etc, etc

Thank you all.

David

------------------
0
 
innovator2Author Commented:
and to zepar,
i think your solution effectively creates two hashtables embedded in one another.
Anyway, I think it will work, though it forces me to do more typing, entering each item twice.

Dave
0
 
jlachCommented:
Im glad you figured it out. You could of also used a dataset and then created a datatalbe inside the dataset with a definied column name and value.. then did a select statement on the datatable to get the value of the second column here the value of the first is equal to the value you want.. (i like datasets, thats why i offer that solution also) =] GOOD LUCK!
0
 
WillyCornbreadCommented:
I don't know if anyone still looks at abandoned questions, but there seems to be a simple way to do this, I needed to do the same..

theKeyYouWant = sortedList.GetKey(sortedList.IndexOfValue(YOURVALUE));

B
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now