Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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