getting values for a given key in hashtable

  • Thread starter Thread starter SenthilVel
  • Start date Start date
SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is as easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());
 
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

Dennis Myrén said:
SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is as easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
how to get the corresponding values for a given Key in hashtable ??
 
SenthilVel said:
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

That suggests that you don't have a value in the hashtable for that key
(or that the value is null).
 
The code assumed you have the key "myKey" in the hashtable.
This example stores a key-value pair in the table, then reads the value of
that key:

Hashtable ht = new Hashtable();
ht.Add("myKey", "myValue");
Console.WriteLine(myHashtable ["myKey"].ToString());

You can iterate through all keys and values in the table like this:
IDictionaryEnumerator e = ht.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(string.Format("Key: {0}, Value: {1}", e.Key.ToString(),
e.Value.ToString()));
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

Dennis Myrén said:
SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is as easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
how to get the corresponding values for a given Key in hashtable ??
 
dennis
can i also do the reverse??
i do have a value and i do need to get the key of that pair??
that too in my condition i have the values duplicating, so i do need to get
the first occurence of the value and teh corresponding Key..
,,,
Senthil



Dennis Myrén said:
The code assumed you have the key "myKey" in the hashtable.
This example stores a key-value pair in the table, then reads the value of
that key:

Hashtable ht = new Hashtable();
ht.Add("myKey", "myValue");
Console.WriteLine(myHashtable ["myKey"].ToString());

You can iterate through all keys and values in the table like this:
IDictionaryEnumerator e = ht.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(string.Format("Key: {0}, Value: {1}", e.Key.ToString(),
e.Value.ToString()));
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
when i use this i get an error like :"Object not set to an instance of ...
Null reference..."

Dennis Myrén said:
SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is as easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
how to get the corresponding values for a given Key in hashtable ??
 
Senthil,
You can do it "the hard way", that is manually looping through the
keys and values and look for that specific value.
Example function:

public object GetCorrespondingKey ( object value, Hashtable hashTable )
{
IDictionaryEnumerator e = hashTable.GetEnumerator();
while (e.MoveNext())
{
if (e.Value == value)
return e.Key;
}
}

Call it like this:
object key = GetCorrespondingKey("myValue", ht);


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
dennis
can i also do the reverse??
i do have a value and i do need to get the key of that pair??
that too in my condition i have the values duplicating, so i do need to
get
the first occurence of the value and teh corresponding Key..
,,,
Senthil



Dennis Myrén said:
The code assumed you have the key "myKey" in the hashtable.
This example stores a key-value pair in the table, then reads the value
of
that key:

Hashtable ht = new Hashtable();
ht.Add("myKey", "myValue");
Console.WriteLine(myHashtable ["myKey"].ToString());

You can iterate through all keys and values in the table like this:
IDictionaryEnumerator e = ht.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(string.Format("Key: {0}, Value: {1}", e.Key.ToString(),
e.Value.ToString()));
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is
as
easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
how to get the corresponding values for a given Key in hashtable ??
 
SenthilVel said:
can i also do the reverse??
i do have a value and i do need to get the key of that pair??
that too in my condition i have the values duplicating, so i do need to get
the first occurence of the value and teh corresponding Key..

What do you mean by "first"? Hashtables aren't ordered (at least, not
in any way you can rely on). If you need to look things up by value, I
suggest you have another map going from value to key.
 
Sorry, a small mistake:
public object GetCorrespondingKey ( object value, Hashtable hashTable )
{
IDictionaryEnumerator e = hashTable.GetEnumerator();
while (e.MoveNext())
{
if (e.Value == value)
return e.Key;
}
// We did not find the value.
return null;
}

object key = GetCorrespondingKey("myValue", ht);
if (key != null)
{
// The value was found, do something with the key.
}

As Jon mentioned, if you do this a lot you should have a keys-values
hashtable as well as
a values-keys table, because this will be a lot more efficient.

You should also bear in mind that while a key is unique in a hashtable, a
value is not.
But maybe that is just the funtionality that you actually want.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Senthil,
You can do it "the hard way", that is manually looping through the
keys and values and look for that specific value.
Example function:

public object GetCorrespondingKey ( object value, Hashtable hashTable )
{
IDictionaryEnumerator e = hashTable.GetEnumerator();
while (e.MoveNext())
{
if (e.Value == value)
return e.Key;
}
}

Call it like this:
object key = GetCorrespondingKey("myValue", ht);


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
SenthilVel said:
dennis
can i also do the reverse??
i do have a value and i do need to get the key of that pair??
that too in my condition i have the values duplicating, so i do need to
get
the first occurence of the value and teh corresponding Key..
,,,
Senthil



Dennis Myrén said:
The code assumed you have the key "myKey" in the hashtable.
This example stores a key-value pair in the table, then reads the value
of
that key:

Hashtable ht = new Hashtable();
ht.Add("myKey", "myValue");
Console.WriteLine(myHashtable ["myKey"].ToString());

You can iterate through all keys and values in the table like this:
IDictionaryEnumerator e = ht.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(string.Format("Key: {0}, Value: {1}", e.Key.ToString(),
e.Value.ToString()));
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

SenthilVel,

Only a single value can be stored with a specific key.
So getting the corresponding value for a given key in a hashtable is
as
easy
as:
string key = "myKey";
object value = myHashtable [key];
Console.WriteLine(value.ToString());



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
how to get the corresponding values for a given Key in hashtable ??
 
Back
Top