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

Calling GetByIndex for a SortedList with Custom Classes.

Hi There.

I have a SortedList collection that I add objects of a custom class
to. Rather then explain my app, my question is pretty general so....

Using a simple clsDog:

***************************
clsDog boomer = new clsDog();
clsDog spot = new clsDog();
clsDog benjy = new clsDog();

SortedList listPets = new SortedList();
listPets.Add ("boomer", boomer);
listPets.Add ("spot", spot);
listPets.Add ("benjy", benjy);
****************************

So, at this point my list has sorted itself and I can verify the
indexes are good (ie, benjy is at index 0, boomer is at 1, spot is at
2).

I realize that the .GetByIndex method has an "object" return type. So,
my understanding is that if I want to return the object at the 1st
index I'd use:

thisDog = (clsDog)listPets.GetByIndex(1);

Unfortunately, this theory is not working out as it appears no matter
the Index that I ask for the collection will always seem to return the
last object I touched (in the above example object benjy is ALWAYS
returned, even though I return the proper key for the 1st index of
"boomer").

I understand that there might be other issues, but after wasting much
of the afternoon I just wanted to make sure I was on the correct
track.

Thanks
Jason
Nov 15 '05 #1
2 2129
arby <go***@yahoo.com> wrote:
I have a SortedList collection that I add objects of a custom class
to. Rather then explain my app, my question is pretty general so....

Using a simple clsDog:

***************************
clsDog boomer = new clsDog();
clsDog spot = new clsDog();
clsDog benjy = new clsDog();

SortedList listPets = new SortedList();
listPets.Add ("boomer", boomer);
listPets.Add ("spot", spot);
listPets.Add ("benjy", benjy);
****************************

So, at this point my list has sorted itself and I can verify the
indexes are good (ie, benjy is at index 0, boomer is at 1, spot is at
2).

I realize that the .GetByIndex method has an "object" return type. So,
my understanding is that if I want to return the object at the 1st
index I'd use:

thisDog = (clsDog)listPets.GetByIndex(1);

Unfortunately, this theory is not working out as it appears no matter
the Index that I ask for the collection will always seem to return the
last object I touched (in the above example object benjy is ALWAYS
returned, even though I return the proper key for the 1st index of
"boomer").


With your code, I can't reproduce the problem. Here's a short but
complete program which *doesn't* demonstrate the problem, but appears
to be basically the same as your code:

using System;
using System.Collections;

public class Dog
{
string name;

public Dog (string name)
{
this.name = name;
}

public override string ToString()
{
return name;
}
}

public class Test
{
static void Main()
{
Dog boomer = new Dog("boomer");
Dog spot = new Dog("spot");
Dog benjy = new Dog("benjy");

SortedList pets = new SortedList();
pets.Add("boomer", boomer);
pets.Add("spot", spot);
pets.Add("benjy", benjy);

Console.WriteLine (pets.GetByIndex(1));
}
}

(Output is "boomer" as expected, rather than "benjy" as you suggest.)

Please post a similar short but complete program which *does*
demonstrate the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi John,

Thanks for the response. Actually, late last night I discovered the
issue and it was unrelated to the list, rather the object getting
passed to the list was never get changed - although the key was
(basically a bug on my end).

Thanks
Jason
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
arby <go***@yahoo.com> wrote:
I have a SortedList collection that I add objects of a custom class
to. Rather then explain my app, my question is pretty general so....

Using a simple clsDog:

***************************
clsDog boomer = new clsDog();
clsDog spot = new clsDog();
clsDog benjy = new clsDog();

SortedList listPets = new SortedList();
listPets.Add ("boomer", boomer);
listPets.Add ("spot", spot);
listPets.Add ("benjy", benjy);
****************************

So, at this point my list has sorted itself and I can verify the
indexes are good (ie, benjy is at index 0, boomer is at 1, spot is at
2).

I realize that the .GetByIndex method has an "object" return type. So,
my understanding is that if I want to return the object at the 1st
index I'd use:

thisDog = (clsDog)listPets.GetByIndex(1);

Unfortunately, this theory is not working out as it appears no matter
the Index that I ask for the collection will always seem to return the
last object I touched (in the above example object benjy is ALWAYS
returned, even though I return the proper key for the 1st index of
"boomer").


With your code, I can't reproduce the problem. Here's a short but
complete program which *doesn't* demonstrate the problem, but appears
to be basically the same as your code:

using System;
using System.Collections;

public class Dog
{
string name;

public Dog (string name)
{
this.name = name;
}

public override string ToString()
{
return name;
}
}

public class Test
{
static void Main()
{
Dog boomer = new Dog("boomer");
Dog spot = new Dog("spot");
Dog benjy = new Dog("benjy");

SortedList pets = new SortedList();
pets.Add("boomer", boomer);
pets.Add("spot", spot);
pets.Add("benjy", benjy);

Console.WriteLine (pets.GetByIndex(1));
}
}

(Output is "boomer" as expected, rather than "benjy" as you suggest.)

Please post a similar short but complete program which *does*
demonstrate the problem.

Nov 15 '05 #3

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

Similar topics

8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
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...
1
by: Vai2000 | last post by:
Hi All, Is there a way to custom sort the SortedList by its value...I know how to do with keys by passing a special Comparable Class...but how do I sort it based on Values? TIA
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<>,...
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:...
3
by: Bruce Wood | last post by:
Sorry if this is a repost. My original post appears to have disappeared. Is it just me, or is the generic SortedList<Tmissing a GetByIndex method. The old (non-generic) SortedList has a method...
7
by: pamela fluente | last post by:
I noticed that I am using quite often SortedLists where I use KEYS only. when I add new elements I use something like : MyKeyObj , Nothing/Null/false Is there a collection type which is...
6
by: n3tx | last post by:
Hi! I have a problem with sortedlist, i guess i dont understand how it works. I have a method called GetPublishingPlaces that returns an IList<PublishingPlace> (ex. contains 11 rows) I want...
3
dcharnigo
by: dcharnigo | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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...
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...

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.