SortedList Custom IComparer NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcharnigo
    New Member
    • Feb 2007
    • 20

    SortedList Custom IComparer NullReferenceException

    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:
    Code:
    using System;
    using System.Collections;
    
    namespace Example
    {
        // An implementation of IComparer.
        public class Int32ComparerClass : IComparer
        {
            public int Compare(object x, object y)
            {
                // This code assumes neither x and y are null,
                // and they both can be successfully converted to Int32s.
                //return Convert.ToInt32(x) - Convert.ToInt32(y);
                return -1;
            }
        }
    
        public class Test
        {
            [STAThread]
            public static void Main()
            {
                SortedList numericList =
                new SortedList(new Int32ComparerClass());
                numericList.Add("10", "Ten");
                numericList.Add("11", "Eleven");
                numericList.Add("1", "One");
                numericList.Add("2", "Two");
                numericList.Add("3", "Three");
                numericList.Add("0", "0");
                numericList.Add("r99", "r99");
                numericList.Add("R99", "R99");
    
                foreach (DictionaryEntry de in numericList)
                    Console.WriteLine(de.Key + ", " + de.Value);
    
                //WILL THROW EXCEPTION
                Console.WriteLine(numericList["1"].ToString());
    
                Console.WriteLine();
    
                SortedList xxx =
                    new SortedList();
                xxx.Add("10", "Ten");
                xxx.Add("11", "Eleven");
                xxx.Add("1", "One");
                xxx.Add("2", "Two");
                xxx.Add("3", "Three");
                xxx.Add("0", "0");
                xxx.Add("r99", "r99");
                xxx.Add("R99", "R99");
    
    
                foreach (DictionaryEntry de in xxx)
                    Console.WriteLine(de.Key + ", " + de.Value);
                
            //IS OK
            Console.WriteLine(xxx["1"].ToString());
    
            }
        }
    }
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    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?

    Comment

    • dcharnigo
      New Member
      • Feb 2007
      • 20

      #3
      Thanks, Apparently I wasn't paying attention, it works fine with a list dictionary! Thanks!

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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.

        Comment

        Working...