Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-30
4
Medium Priority
?
1,093 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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