473,406 Members | 2,352 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,406 software developers and data experts.

c# SortedList can't inserted items,even not repeated

24
In the folling codes, Product is a class with SortedList Memeber Components.

Expand|Select|Wrap|Line Numbers
  1. Product prod = new Product();   
  2. prod.Components = new sortedList( new IntComparer() );   
  3. prod.Components.Add(0,1);   
  4. prod.Components.Add(1,2);   
  5.  
  6. private class IntComparer : Comparer <int>             
  7.        public override int Compare(int x, int y) 
  8.        { 
  9.                if   (x   <   y) 
  10.                     return   0; 
  11.                else   if   (x   ==   y) 
  12.                     return   0; 
  13.                else   return   -1; 
  14.         } 
  15. }
The Second Add(1,2) cause an error,

Item has already been added. Key in dictionary: "0" Key being added: "1".

Since 0 and 1 are different key, why this error occur?

In fact these codes had run correctly several days before, may be there are something error elsewhere ?
Jan 23 '08 #1
2 1501
mentor
24
it's solved. there is something wrong with the comparer
Jan 23 '08 #2
Plater
7,872 Expert 4TB
Hehe yeah. Never returned a 1.
Jan 23 '08 #3

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

Similar topics

3
by: studen77 | last post by:
I'm not comprehending how C# assigns indexes to items you add to a SortedList item. Here's some example code: sl.Add ("first_item","First Item"); sl.Add ("second_item", "Second Item"); sl.Add...
2
by: Pekka | last post by:
Could somebody say why the piece of code below does not work? My purpose is to renumber keys in a SortedList (after removal of an item) so that the keys would always contain an unbroken sequence of...
0
by: andreas | last post by:
Hi, I have filled a sortedlist with items of instances of a class and the corresponding keys Now i will have back all the keys and the corresponding properties of the items dim keyitem as...
2
by: Georg | last post by:
Hi, I would like to use a SortedList collection to store numeric data items with a key (numeric location). When I have a key which is not stored in the SortedList and which should not be stored...
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...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
4
by: Michael Nesslinger | last post by:
Hello, i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" for a SortedList like it is possible for a List. My first question is: Why is the Method not available for the...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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.