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 said:
Jose Jarabo said:
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.