473,732 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SortedList Custom IComparer NullReferenceEx ception

dcharnigo
20 New Member
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 4473
nukefusion
221 Recognized Expert New Member
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
20 New Member
Thanks, Apparently I wasn't paying attention, it works fine with a list dictionary! Thanks!
Dec 16 '08 #3
Plater
7,872 Recognized Expert Expert
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
2766
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 Guid.NewGuid().ToString() is appended to make sure the key is unique in case of duplicated data.
1
20543
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 key-value pairs of UserID - DisplayName for a whole bunch of user objects. The UserID is simply a long, the DisplayName is something like, "Jones, Bill". I want the SortedList to sort the names alphabetically, rather than by
3
6192
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 add to the list, it only seems to sort alphabetically. Thanks
3
2817
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
2370
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 that I could make the key for my sorted list the LastWriteTime of the file. Apart from this i'm at a real loose end. Any clues anyone?
1
1271
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 poemfile As String In poemfiles Poems.Add(System.IO.File.OpenText(poemfile).ReadLine(), poemfile)
4
10125
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<>, both works. private void Form1_Load(object sender, EventArgs e) { System.Collections.SortedList QA1 = new System.Collections.SortedList();
6
1538
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("??", "??") Debugging this peace of code I have notice that '??' appears as first item,
4
6766
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 SortedList like this: class OtherSortedList : SortedList { ..... int GetTotalAmountOfAllItems().... }
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.