[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1099
  • Last Modified:

C#: Hashtable vs. SortedList?????

I thought Hashtable and SortedList where identical except for the specific sorting features of a SortedList! So the code I used to make the Hashtable work does not work with a SortedList.

Q. Why does the SortedList code return an error: object reference not set to the instance of an object.. ???



/////// HashTable  ////////// Code works fine...

if (hash.ContainsKey(str_col_1.Trim()))
{
strDestDns = hash[str_col_1.Trim()].ToString();      
}
else
{
strDest1 = "No Match Found";
}

///////  SortedList  //////////// Code does NOT work fine....????

if(list.ContainsKey(str_col_1.Trim()))
{
strDest1 = list[str_col_1.Trim()].ToString();
}
else
{
strDest1 = "No Match Found";


0
kvnsdr
Asked:
kvnsdr
  • 2
  • 2
1 Solution
 
gregoryyoungCommented:
and aside from the fact that a hashtable implements a hashtable ...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsidictionaryclasstopic.asp

if you want to experiment with both you should refer to the object as an IDictionary (both HashTable and SortedList implement this therefor you can use either interchangably).

I believe that you are most likely failing

if(list.ContainsKey(str_col_1.Trim()))

make sure that list is set to an object ... i.e. its not null ...


IDictionary dictionary = list or hash .....

if(dictionary.Contains(str_col_1.Trim()))
{
strDest1 = dictionary.Item[str_col_1.Trim()].ToString();
}
else
{
strDest1 = "No Match Found";
0
 
God_AresCommented:
if your doing xml stuff try this code

try
                  {

                        XmlTextReader r = new XmlTextReader("http://www.w3schools.com/xml/note.xml");                  
                        XmlDocument d = new XmlDocument();
                        d.Load(r);                              
                        XmlNode node = d.SelectSingleNode("/note/to");                  
                        if (node!=null) MessageBox.Show(node.InnerText);
                        r.Close();
                  }
                  catch(System.Net.WebException ex)
                  {
                        MessageBox.Show("Web exception  :"+ex.Message);
                  }
                  catch(System.Xml.XmlException ex)
                  {
                        MessageBox.Show("Xml exception :"+ex.Message);
                  }
                  catch(System.Exception ex)
                  {
                        MessageBox.Show("General exception :"+ex.Message);
                  }


but i think you just want some data read from a page,.. so here :

copy paste from my own project,.. so there are some controls and texts you want to handle diffrently,...

HttpWebRequest HttpWReq =
                        (HttpWebRequest)WebRequest.Create("http://"+textBox1.Text);

                  HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                  
                  Stream receiveStream = HttpWResp.GetResponseStream();
                  System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                  // Pipes the stream to a higher level stream reader with the required encoding format.
                  StreamReader readStream = new StreamReader( receiveStream, encode );
                  textBox2.Text = "\r\nResponse stream received.\r\n";
                  Char[] read = new Char[256];
                  // Reads 256 characters at a time.    
                  int count = readStream.Read( read, 0, 256 );
                  textBox2.Text += "HTML...\r\n";
                  while (count > 0)
                  {
                        // Dumps the 256 characters on a string and displays the string to the console.
                        String str = new String(read, 0, count);
                        textBox2.Text += str;
                        count = readStream.Read(read, 0, 256);
                  }
                  


                  HttpWResp.Close();
0
 
God_AresCommented:
hmm posted this code on the wrong page,.. sorry!
0
 
gregoryyoungCommented:
I was wonderring :)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now