473,761 Members | 8,463 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 4475
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
2767
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
20546
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
6195
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
2819
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
1274
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
1539
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
6767
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
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
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...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
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.