Hashtable myHash = new Hashtable(); myHash.Add(1, "word1"); myHash.Add(2, "word2"); Now I want to get the value for a given key, for example the first key is given. How could I do this? I tried string test = myHash.Item(1); // but this line failed to compile
I can not find other appropriate methods that I can call to retrieve my value for a given key.
Unfortunately, it is not working. An exception was thrown saying "the object referrenced is not instantiated". I tried also int n = 1; string test = myHash[n].ToString(); Same thing happens. Any idea?
OK, just now I tried below code in page_load it worked
Hashtable myHash = new Hashtable();
myHash.Add(1, "word1");
myHash.Add(2, "word2");
int n = 1;
string test = myHash[n].ToString();
Response.Write(test);
Can you please let me know on which line you got the problem!! Also you are immidiately using myHash after Add or you are using in different place???
Thanks for your test out the code. Let me be more specific I created a class called Constants that holds a Hashtable public variable. public class Constants { public Hashtable poStates; public Constants() { // init poStates = new Hashtable(); poStates.Add (1
, "word1"); poStates.Add (2, "word2"); } } In a page's code behind I call it this way: Constants con = new Constants (); int n = 1 string test = con.poStates[n].ToString (); It crashed on the line of the caller code. Don't know why, I called the constructor
of the Constants and the Hashtable should be populated correctly. Any ideas?
OK, Code seems fine, I had copied your code and tested, it worked for me! I would think only one thing, if you are not able to access Constants and if you are using Constants in your page, you will get the error in Constants con = new Constants (); line. Also
remember to keep ; in the line int n = 1 , because, when I copy your code, I got the error in only that line, if I added semicolan, everything worked fine to me. All you have to look into is, is Constants is the scope of used codebehind page.
Omitting ';" for n = 1 is a typo. The code still does not work for me. I added the following line Constants con = new Constants (); int nCount = con.poStates.Count; int nKey = 1; string test = con.poStates[nKey].ToString (); I debug it and found nCount has
correct value, so that the Hashtable itself is OK, but it still crash on the last line. On another test, I tried to transfer all the code in Constants class into my code behind, but compiler does not recognise HastTable keyword even though I have added System.Collection
namespace to the page. I do not know why? Could this be the problem?
Not recognising Hashtable is because I typed "HashTable", I forgot this is c# world, case sensitive world. The code became working after I transfer all the Hashtable code from Constants to the page's code behind. MyHashtable[nKey] did get the value. So, the
problem lies in the usage of Constants class, which I do not know what I did wrong.
Charles Bai
Member
675 Points
135 Posts
get value from Hashtable for a given key
Jul 14, 2004 11:32 PM|LINK
SreedharK
All-Star
18571 Points
3119 Posts
MVP
Re: get value from Hashtable for a given key
Jul 14, 2004 11:48 PM|LINK
http://www.w3coder.org
weblog http://weblogs.asp.net/skoganti
Charles Bai
Member
675 Points
135 Posts
Re: get value from Hashtable for a given key
Jul 15, 2004 01:10 AM|LINK
SreedharK
All-Star
18571 Points
3119 Posts
MVP
Re: get value from Hashtable for a given key
Jul 15, 2004 01:40 AM|LINK
Hashtable myHash = new Hashtable(); myHash.Add(1, "word1"); myHash.Add(2, "word2"); int n = 1; string test = myHash[n].ToString(); Response.Write(test);Can you please let me know on which line you got the problem!! Also you are immidiately using myHash after Add or you are using in different place???http://www.w3coder.org
weblog http://weblogs.asp.net/skoganti
Charles Bai
Member
675 Points
135 Posts
Re: get value from Hashtable for a given key
Jul 15, 2004 02:26 AM|LINK
SreedharK
All-Star
18571 Points
3119 Posts
MVP
Re: get value from Hashtable for a given key
Jul 15, 2004 10:12 AM|LINK
http://www.w3coder.org
weblog http://weblogs.asp.net/skoganti
Charles Bai
Member
675 Points
135 Posts
Re: get value from Hashtable for a given key
Jul 15, 2004 02:06 PM|LINK
Charles Bai
Member
675 Points
135 Posts
Re: get value from Hashtable for a given key
Jul 15, 2004 02:45 PM|LINK