473,388 Members | 1,032 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,388 software developers and data experts.

SortedList bug?

Hello, thanks in advance for any and all replies.

I am not sure how to categorize this, either a bug or something else but
here is my problem.

I am testing with 2 elements on a sorted list. If I remove the last element
on the list everything works fine and I can add other elements to it. However
if I remove the first element on the list and I try to add something then my
first element is changed when I add a new element to that new element.
Here is how im adding elements

SomeDataType newlement = new SomeDataType();
SomeSortedList[SomeSortedList.Count] = newelement;

this is how I remove them.

SomeSortedList.RemoveAt(index);

I have reviewed the code over and over and the element is being correctly
removed but apparently when the list is resorted a pointer to the element
that moved to the empty spot remains in its original spot..

Maybe im wrong and that hit i took to the head earlier has me spinning.
Anyways if anyone can tell me.. please let me know.
Nov 16 '05 #1
6 1853
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
I am not sure how to categorize this, either a bug or something else but
here is my problem.

I am testing with 2 elements on a sorted list. If I remove the last element
on the list everything works fine and I can add other elements to it. However
if I remove the first element on the list and I try to add something then my
first element is changed when I add a new element to that new element.
Here is how im adding elements

SomeDataType newlement = new SomeDataType();
SomeSortedList[SomeSortedList.Count] = newelement;

this is how I remove them.

SomeSortedList.RemoveAt(index);

I have reviewed the code over and over and the element is being correctly
removed but apparently when the list is resorted a pointer to the element
that moved to the empty spot remains in its original spot..

Maybe im wrong and that hit i took to the head earlier has me spinning.
Anyways if anyone can tell me.. please let me know.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Here is some code that generates the problem.. Thank you. In the output u can
see the problem generated.

using System;
using System.Collections;
namespace SortedListProblem
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

GroupMan Manager = new GroupMan();
Manager.AddGroup("Group1");
Manager.AddMember("Group1", "MemberName1", "MemberID1");
Manager.AddMember("Group1", "MemberName2", "MemberID2");
Manager.AddMember("Group1", "MemberName3", "MemberID3");

Manager.PrintOutGroups();
Manager.RemoveMember("Group1", "MemberName1", "MemberID1");
Manager.PrintOutGroups();
Manager.AddMember("Group1", "MemberName4", "MemeberID4");
Manager.PrintOutGroups();
}
}
public class GroupBase
{
public SortedList Members = new SortedList(20);
private string GroupName;

public GroupBase(string name)
{
//
// TODO: Add constructor logic here
//
GroupName = name;

}

public void ChangeName(string name)
{
GroupName = name;
}
public string GetName()
{
return GroupName;
}

public void AddMember(string name, string ID)
{
MemberBase temp = new MemberBase(name, ID, GenerateID());
Console.WriteLine("Adding to index: "+ Members.Count);
Members[Members.Count] = temp;
Console.WriteLine("Member "+ temp.Name+" with ID "+temp.ID+" Added to
Group "+ GroupName+'\n' +" Located At Sorted List Location "+
System.Convert.ToString(Members.Count-1));

}
public void Printout()
{
Console.WriteLine("Members in Group "+GroupName+" are:");

for(int i =0; i < Members.Count; i++)
{
MemberBase temp = (MemberBase)Members.GetByIndex(i);
Console.WriteLine("Name: "+temp.Name);
Console.WriteLine("ID: "+temp.ID);
Console.WriteLine("SortedList index: "+System.Convert.ToString(i));
System.Threading.Thread.Sleep(500);
}
}
public void RemoveMember(string name, string ID)
{
for( int i =0; i < Members.Count;i++)
{
MemberBase temp = (MemberBase) Members.GetByIndex(i);
if(temp.Name == name&& temp.ID == ID)
{
Members.RemoveAt(i);
return;
}
}
}
public Guid GenerateID()
{
Guid guid = Guid.NewGuid();
return guid;
}
}

public class GroupMan
{
private SortedList Groups = new SortedList();
public SortedList GroupNames = new SortedList();
public GroupMan()
{
//
// TODO: Add constructor logic here
//
}

public void AddGroup(string Name)
{
GroupBase temp = new GroupBase(Name);
Groups[Groups.Count] = temp;

GroupNames[GroupNames.Count] = Name;
Console.Write("Group: "+temp.GetName()+" Added\n");

}
public void AddMember(string GName, string MName, string MID)
{
GroupBase temp;
for(int i = 0; i < Groups.Count; i++)
{
temp = (GroupBase)Groups.GetByIndex(i);
if(temp.GetName()==GName)
{
temp.AddMember(MName, MID);
}

}
System.Threading.Thread.Sleep(500);

}
public void PrintOutGroups()
{
Console.WriteLine("PRINTING OUT GROUPS!!!");
for(int i =0; i < Groups.Count; i++)
{
GroupBase temp = (GroupBase)Groups.GetByIndex(i);
temp.Printout();
System.Threading.Thread.Sleep(500);
}
}
public void RemoveMember(string GName,string Name, string ID)
{
for(int i = 0; i < Groups.Count;i++)
{
GroupBase temp = (GroupBase)Groups.GetByIndex(i);
if(temp.GetName()==GName)
{
temp.RemoveMember(Name,ID);
}

}
}


}

public class MemberBase
{
public string Name;
public string ID;
public Guid GUID;
public MemberBase(string tempName, string tempID, Guid GID)
{
//
// TODO: Add constructor logic here
//
Name = tempName;
ID = tempID;
GUID = GID;
}
}
}
"Jon Skeet [C# MVP]" wrote:
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
I am not sure how to categorize this, either a bug or something else but
here is my problem.

I am testing with 2 elements on a sorted list. If I remove the last element
on the list everything works fine and I can add other elements to it. However
if I remove the first element on the list and I try to add something then my
first element is changed when I add a new element to that new element.
Here is how im adding elements

SomeDataType newlement = new SomeDataType();
SomeSortedList[SomeSortedList.Count] = newelement;

this is how I remove them.

SomeSortedList.RemoveAt(index);

I have reviewed the code over and over and the element is being correctly
removed but apparently when the list is resorted a pointer to the element
that moved to the empty spot remains in its original spot..

Maybe im wrong and that hit i took to the head earlier has me spinning.
Anyways if anyone can tell me.. please let me know.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
Here is some code that generates the problem.. Thank you. In the output u can
see the problem generated.


Okay, I see the problem. You're reusing a key, namely the number 2.

Originally, the key for MemberID3 is 2, because the size of the list
before you add it is 2. However, that's *also* true when you add
MemberID4, so it overwrites MemberID3.

Basically, you shouldn't be using the size of the list as the key. What
do you *really* want it to be sorted by?

For future reference, one of the key words for making a problem easy to
understand is to make the example *short*. Here's a significantly
shorter and simpler program demonstrating your problem:

using System;
using System.Collections;

class Test
{
static void Main()
{
SortedList list = new SortedList();

AddEntry (list, "First");
AddEntry (list, "Second");
AddEntry (list, "Third");
DumpList(list);
RemoveEntry (list, "First");
DumpList(list);
AddEntry (list, "Fourth");
DumpList(list);
}

static void DumpList(SortedList list)
{
Console.WriteLine ("List contents:");
foreach (DictionaryEntry entry in list)
{
Console.WriteLine ("{0}: {1}", entry.Key, entry.Value);
}
}

static void RemoveEntry (SortedList list, string value)
{
Console.WriteLine ("Removing {0}", value);
for (int i=0; i < list.Count; i++)
{
if (value.Equals(list.GetByIndex(i)))
{
list.RemoveAt(i);
break;
}
}
}

static void AddEntry (SortedList list, string value)
{
Console.WriteLine ("Adding {0}", value);
list[list.Count] = value;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I was under the impression that once an element is removed from the list the
element indexed after the element removed is moved up on the list and takes
its index. I thought that when i used [] i was placing an element by an index
and not a key.

"Jon Skeet [C# MVP]" wrote:
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
Here is some code that generates the problem.. Thank you. In the output u can
see the problem generated.


Okay, I see the problem. You're reusing a key, namely the number 2.

Originally, the key for MemberID3 is 2, because the size of the list
before you add it is 2. However, that's *also* true when you add
MemberID4, so it overwrites MemberID3.

Basically, you shouldn't be using the size of the list as the key. What
do you *really* want it to be sorted by?

For future reference, one of the key words for making a problem easy to
understand is to make the example *short*. Here's a significantly
shorter and simpler program demonstrating your problem:

using System;
using System.Collections;

class Test
{
static void Main()
{
SortedList list = new SortedList();

AddEntry (list, "First");
AddEntry (list, "Second");
AddEntry (list, "Third");
DumpList(list);
RemoveEntry (list, "First");
DumpList(list);
AddEntry (list, "Fourth");
DumpList(list);
}

static void DumpList(SortedList list)
{
Console.WriteLine ("List contents:");
foreach (DictionaryEntry entry in list)
{
Console.WriteLine ("{0}: {1}", entry.Key, entry.Value);
}
}

static void RemoveEntry (SortedList list, string value)
{
Console.WriteLine ("Removing {0}", value);
for (int i=0; i < list.Count; i++)
{
if (value.Equals(list.GetByIndex(i)))
{
list.RemoveAt(i);
break;
}
}
}

static void AddEntry (SortedList list, string value)
{
Console.WriteLine ("Adding {0}", value);
list[list.Count] = value;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
I was under the impression that once an element is removed from the list the
element indexed after the element removed is moved up on the list and takes
its index. I thought that when i used [] i was placing an element by an index
and not a key.


No, it's using a key. That's why the Item property's parameter is of
type object, not int.

SortedList is sorted by key - if you didn't think the index was the
key, what did you think *was* the key?

If you don't really need it to be sorted by anything other than order
in which it's added, you should probably just use ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Well to be honest i just jumped into C# and .NET about a month ago from C++.
Still learning the ropes on all the new tools available to me. I thought I
could arrange things in the SortedList both by Key or Index, i guess i was
wrong =). Thank you very much for your help, and sorry my code was too long
just wanted to make sure I included all the relevant things that comprised
the program but I guess i over did it a bit =).

"Jon Skeet [C# MVP]" wrote:
Jose Jarabo <Jo********@discussions.microsoft.com> wrote:
I was under the impression that once an element is removed from the list the
element indexed after the element removed is moved up on the list and takes
its index. I thought that when i used [] i was placing an element by an index
and not a key.


No, it's using a key. That's why the Item property's parameter is of
type object, not int.

SortedList is sorted by key - if you didn't think the index was the
key, what did you think *was* the key?

If you don't really need it to be sorted by anything other than order
in which it's added, you should probably just use ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

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

Similar topics

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...
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<>,...
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:...
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...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...

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.