By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

SortedList Custom IComparer NullReferenceException

dcharnigo
P: 20
I thought I would be tricky and force the SortedList to not sort using a custom IComparer, I used the following code, but when the list is sorting using the custom sort the accessors become unusable and throw exceptions. I have since discovered that the accessors use the IComparer to find things and since it is set at -1 it gets a null return value, however the foreach loops dont use it. I must preserve the insert order, I don't want the sortinglist to sort, is there any way or type to accomplish this? A ListDictionary does not have the accessors I need. I also need it to be Case-Sensitive meaning for keys I have some that are 'r99' and 'R99' and that they are not the same. (Loading and XML file and XML is Case-Sensitive)

Thanks


Sample Application:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Example
  5. {
  6.     // An implementation of IComparer.
  7.     public class Int32ComparerClass : IComparer
  8.     {
  9.         public int Compare(object x, object y)
  10.         {
  11.             // This code assumes neither x and y are null,
  12.             // and they both can be successfully converted to Int32s.
  13.             //return Convert.ToInt32(x) - Convert.ToInt32(y);
  14.             return -1;
  15.         }
  16.     }
  17.  
  18.     public class Test
  19.     {
  20.         [STAThread]
  21.         public static void Main()
  22.         {
  23.             SortedList numericList =
  24.             new SortedList(new Int32ComparerClass());
  25.             numericList.Add("10", "Ten");
  26.             numericList.Add("11", "Eleven");
  27.             numericList.Add("1", "One");
  28.             numericList.Add("2", "Two");
  29.             numericList.Add("3", "Three");
  30.             numericList.Add("0", "0");
  31.             numericList.Add("r99", "r99");
  32.             numericList.Add("R99", "R99");
  33.  
  34.             foreach (DictionaryEntry de in numericList)
  35.                 Console.WriteLine(de.Key + ", " + de.Value);
  36.  
  37.             //WILL THROW EXCEPTION
  38.             Console.WriteLine(numericList["1"].ToString());
  39.  
  40.             Console.WriteLine();
  41.  
  42.             SortedList xxx =
  43.                 new SortedList();
  44.             xxx.Add("10", "Ten");
  45.             xxx.Add("11", "Eleven");
  46.             xxx.Add("1", "One");
  47.             xxx.Add("2", "Two");
  48.             xxx.Add("3", "Three");
  49.             xxx.Add("0", "0");
  50.             xxx.Add("r99", "r99");
  51.             xxx.Add("R99", "R99");
  52.  
  53.  
  54.             foreach (DictionaryEntry de in xxx)
  55.                 Console.WriteLine(de.Key + ", " + de.Value);
  56.  
  57.         //IS OK
  58.         Console.WriteLine(xxx["1"].ToString());
  59.  
  60.         }
  61.     }
  62. }
  63.  
Dec 16 '08 #1
Share this Question
Share on Google+
3 Replies


nukefusion
Expert 100+
P: 221
Hi, thanks for taking the time out to write a sample program. The code sample you provided should work fine using a ListDictionary. Which accessors are missing from ListDictionary that make it unusable for you?
Dec 16 '08 #2

dcharnigo
P: 20
Thanks, Apparently I wasn't paying attention, it works fine with a list dictionary! Thanks!
Dec 16 '08 #3

Plater
Expert 5K+
P: 7,872
I was going to say, you are using a SortedList, but not wanting Sorting and that there must be a better alternative. Glad you found it.
Dec 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.