By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

getting values for a given key in hashtable

P: n/a
how to get the corresponding values for a given Key in hashtable ??

Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <se******************@misyshealthcare.com> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
how to get the corresponding values for a given Key in hashtable ??

Nov 16 '05 #2

P: n/a
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:%D*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
how to get the corresponding values for a given Key in hashtable ??


Nov 16 '05 #3

P: n/a
SenthilVel <se******************@misyshealthcare.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
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" <se******************@misyshealthcare.com> wrote in message
news:ui*************@TK2MSFTNGP12.phx.gbl...
when i use this i get an error like :"Object not set to an instance of ..
Null reference..."

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:%D*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
> how to get the corresponding values for a given Key in hashtable ??
>
>
>



Nov 16 '05 #5

P: n/a
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" <de****@oslokb.no> wrote in message
news:IY*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:ui*************@TK2MSFTNGP12.phx.gbl...
when i use this i get an error like :"Object not set to an instance of ... Null reference..."

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:%D*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message news:Os**************@TK2MSFTNGP12.phx.gbl...
> how to get the corresponding values for a given Key in hashtable ??
>
>
>



Nov 16 '05 #6

P: n/a
SenthilVel <se******************@misyshealthcare.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
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" <se******************@misyshealthcare.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
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" <de****@oslokb.no> wrote in message
news:IY*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:ui*************@TK2MSFTNGP12.phx.gbl...
> when i use this i get an error like :"Object not set to an instance of .. > Null reference..."
>
> "Dennis Myrén" <de****@oslokb.no> wrote in message
> news:%D*****************@news2.e.nsc.no...
>> 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" <se******************@misyshealthcare.com> wrote in message >> news:Os**************@TK2MSFTNGP12.phx.gbl...
>> > how to get the corresponding values for a given Key in hashtable ??
>> >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #8

P: n/a
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" <de****@oslokb.no> wrote in message
news:%D*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
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" <de****@oslokb.no> wrote in message
news:IY*****************@news2.e.nsc.no...
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" <se******************@misyshealthcare.com> wrote in message
news:ui*************@TK2MSFTNGP12.phx.gbl...
> when i use this i get an error like :"Object not set to an instance of

..
> Null reference..."
>
> "Dennis Myrén" <de****@oslokb.no> wrote in message
> news:%D*****************@news2.e.nsc.no...
>> 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" <se******************@misyshealthcare.com> wrote in

message
>> news:Os**************@TK2MSFTNGP12.phx.gbl...
>> > how to get the corresponding values for a given Key in hashtable ??
>> >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.