Can use use value to find key (HashTable)

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a hash table like:
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

We can use key to find value like:
myHT["First"] -> "Hello"

But how can use use value to find key?
 
ad,

You will have to maintain another hashtable which places the value in
for the key, and the key for the value in order to do a reverse lookup like
this.

Hope this helps.
 
Hi,

There is nothing for this in the Hashtable, you have to iterate in it to get
the key.

You could extend the Hashtable and include this as a new method.


cheers,
 
Thank for your answer!
According your answer, I iterate the value to get the key with the code
below

public string FindKey(myValue)
{
string myKey="";

foreach (string aKey in myHT.Keys)
{
if (myHT(aKey)=myValue)
myKey=aKey
}
return myKey
}

But how can I extend this method to Hashtable?




Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

There is nothing for this in the Hashtable, you have to iterate in it to
get the key.

You could extend the Hashtable and include this as a new method.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



ad said:
I have a hash table like:
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

We can use key to find value like:
myHT["First"] -> "Hello"

But how can use use value to find key?
 
And before iterating thru the HashTable, check for the existence of the value being searched using the ContainsValue() method of the HashTable type.

To implement the needed functionality you will have a write a class which derives from the HashTable type and probably write an overload of the ContainsValue() method which also returns the key as an output parameter if the value can be found.

public class MyCustomHashTable : Hashtable
{
public bool ContainsValue(object Value, out string key)
{
if (this.ContainsValue(Value))
{
//iterate and get the key
....
}
else
{
key = String.Empty;
}
....
}
}

HTH, Metallikanz!

ad said:
Thank for your answer!
According your answer, I iterate the value to get the key with the code
below

public string FindKey(myValue)
{
string myKey="";

foreach (string aKey in myHT.Keys)
{
if (myHT(aKey)=myValue)
myKey=aKey
}
return myKey
}

But how can I extend this method to Hashtable?




Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

There is nothing for this in the Hashtable, you have to iterate in it to
get the key.

You could extend the Hashtable and include this as a new method.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



ad said:
I have a hash table like:
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

We can use key to find value like:
myHT["First"] -> "Hello"

But how can use use value to find key?
 
This has been extremely helpful, thanks for all the posts. Here is the
code that I have written to do this.

public class CustomHashtable:Hashtable
{
public CustomHashtable()
{
//
// TODO: Add constructor logic here
//
}

public bool ContainsValue(object Value, out int key)
{
if (this.ContainsValue(Value))
{
//iterate and get the key
foreach (int aKey in Keys)
{
if (this[aKey].Equals(Value)==true)
{
key=aKey;
return true;
}
}
}
else
{
key = -1;
return false;
}
key = -1;
return false;

}
}
 
Back
Top