Solved

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

Posted on 2004-08-30
4
1,073 Views
Last Modified: 2008-01-09
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
Comment
Question by:kvnsdr
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 11934625
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
 
LVL 7

Expert Comment

by:God_Ares
ID: 11949948
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
 
LVL 7

Expert Comment

by:God_Ares
ID: 11962382
hmm posted this code on the wrong page,.. sorry!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11963482
I was wonderring :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question