?
Solved

HOW DO I RETURN A KEY FROM A HASHTABLE/SORTEDLIST

Posted on 2003-02-23
12
Medium Priority
?
360 Views
Last Modified: 2010-04-15
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
Comment
Question by:innovator2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:Sijin
ID: 8007176
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
 

Author Comment

by:innovator2
ID: 8008161
thank you.
I thought of that but I felt there might be a better way.
Really appreciate it.

Dave
0
 

Expert Comment

by:zepar
ID: 8010212
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:innovator2
ID: 8014020
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
 

Accepted Solution

by:
zepar earned 200 total points
ID: 8016663
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
 
LVL 1

Expert Comment

by:jlach
ID: 8017024
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
 

Author Comment

by:innovator2
ID: 8167630
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
 

Author Comment

by:innovator2
ID: 8167654
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
 
LVL 1

Expert Comment

by:jlach
ID: 8168311
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
 

Expert Comment

by:WillyCornbread
ID: 13787110
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question