471,072 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,072 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 1753
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gerrod | last post: by
2 posts views Thread by Prez | last post: by
4 posts views Thread by SHEBERT | last post: by
6 posts views Thread by n3tx | last post: by

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.