Solved

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

Posted on 2004-08-30
4
1,061 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now