473,396 Members | 1,814 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.

Problem with SortedList

Hi,

I am using SortedList, but I am having problems with indexing.

SortedList x = new SortedList();

x.Add(0, "zero");
x.Add(1, "one");
x.Add(3, "three");
Console.WriteLine(x[x.IndexOfKey(0)].ToString());
Console.WriteLine(x[x.IndexOfKey(1)].ToString());
int index = x.IndexOfKey(3);
Console.WriteLine(index);
Console.ReadLine();
Console.WriteLine(x[index].ToString());

The first two writeline work ok, but when I try to retrieve the index
of the key 3, Index of 2 is returned.

Now when I try to use this index(ie index=2) to retrieve the data from
the sortedList, a NullReferenceException is given.
Can someone help me out.
Thanks in Advance

Nov 17 '05 #1
2 1504

Curious wrote:
Hi,

I am using SortedList, but I am having problems with indexing.

SortedList x = new SortedList();

x.Add(0, "zero");
x.Add(1, "one");
x.Add(3, "three");
Console.WriteLine(x[x.IndexOfKey(0)].ToString());
Console.WriteLine(x[x.IndexOfKey(1)].ToString());
int index = x.IndexOfKey(3);
Console.WriteLine(index);
Console.ReadLine();
Console.WriteLine(x[index].ToString());

The first two writeline work ok, but when I try to retrieve the index
of the key 3, Index of 2 is returned.

Now when I try to use this index(ie index=2) to retrieve the data from
the sortedList, a NullReferenceException is given.


..Item, the indexer property of a SortedList, takes a *key*, not an
index. Therefore when you ask for x[2] you are asking for the value
associated with the *key* 2 - there isn't one, so you get null back.

To get by index, use the .GetByIndex method.
--
Larry Lard
Replies to group please

Nov 17 '05 #2
Thanks, infact it requests for the object key in the indexer property

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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
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...
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...
2
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I...
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<>,...
4
by: sck10 | last post by:
Hello, I have a method in my codefile that builds a sorted list (see CodeFile). I am trying to create a class that does the same thing (see App_Code). CodeFile...
4
by: aeshiels | last post by:
Hello, I have a SortedList defined as... SortedList<CUser, CUseruserList = new SortedList<CUser, CUser>(); ....and which to copy it to another sorted list SortedList<CUser,...
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:...
1
by: asem0525 | last post by:
Hello guys, I have this kind of problem: first, the textBox tbISBN has a value that is read from the text file, but then after pressing the "Clear" button, it should be realized, and when I enter...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.