473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SortedList Custom IComparer NullReferenceException

dcharnigo
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
3 4452
nukefusion
221 Expert 100+
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
Thanks, Apparently I wasn't paying attention, it works fine with a list dictionary! Thanks!
Dec 16 '08 #3
Plater
7,872 Expert 4TB
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

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

Similar topics

2
by: Zeng | last post by:
Would someone know why SortedList doesn't work for these two keys? System.Collections.SortedList.Add(Object key, Object value) +221 My code looks something like below, the...
1
by: gerrod | last post by:
Hi - Does anyone know a way to created a SortedList (in the System.Collections namespace) that will sort on VALUES instead of KEYS... ? The scenario is this - I have a SortedList containing...
3
by: Johannes | last post by:
I've read that the SortedList object can sort its elements in alphabetical or numerical order. If this is correct, how can I set the sort order to numerical. Regardless of the key/value pairs I...
3
by: Michael C | last post by:
Hi all, I'm using a SortedList to store data, and want the keys to be compared in case insensitive order, so that mySList is the same as mySList
4
by: CodeRazor | last post by:
Hi, I want to retrieve a sorted list of files, ordered by LastWriteTime. I know I can implement IComparer, but I don't know how or what this means. I know a sortedlist has objects and keys and...
1
by: Nathan Sokalski | last post by:
I am trying to make a SortedList using the following code: Dim Poems As New SortedList(47) Dim poemfiles As String() = System.IO.Directory.GetFiles(Server.MapPath("poetry/poems/")) For Each...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
6
by: Antonio Paglia | last post by:
Hello. I have tried to insert this items into a SortedList. dic = New SortedList dic.Add("<<", "<<") dic.Add("==", "==") dic.Add(">>", ">>") dic.Add("@@", "@@") dic.Add("??", "??") ...
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.