S
SenthilVel
how to get the corresponding values for a given Key in hashtable ??
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..."
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 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 ??
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..
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 ??