473,668 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)listPet s.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 2145
arby <go***@yahoo.co m> 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)listPet s.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.Collecti ons;

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("boome r", boomer);
pets.Add("spot" , spot);
pets.Add("benjy ", benjy);

Console.WriteLi ne (pets.GetByInde x(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.co m>
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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
arby <go***@yahoo.co m> 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)listPet s.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.Collecti ons;

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("boome r", boomer);
pets.Add("spot" , spot);
pets.Add("benjy ", benjy);

Console.WriteLi ne (pets.GetByInde x(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
3121
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 field and an event that is fired if the boolean field value changes. The derived SortedList class has an override for the Add method. The Add method adds an event handler for the object's event for each new item added to the list. In the...
1
20529
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
1
1534
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
10117
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();
1
3898
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: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
3
439
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 that, given an index into the sorted list, will return the value of the item at that index. The new SortedList<Thas methods to get the index into the list of a value, and another methdo to get the index into the list of a key
7
2259
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 similar to the SortedList by does NOT have the Values ?
6
2509
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 to sort the list in different orders, I dont know if this is something that SortedList do ? i have a property in the PublishingPlace called Place, name, exposure
3
4470
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 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...
0
8459
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
8371
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
8889
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
8790
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
8572
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
8652
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...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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.