Calling GetByIndex for a SortedList with Custom Classes.

A

arby

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
 
J

Jon Skeet [C# MVP]

arby said:
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.
 
A

arby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top