SortedList Issue

K

KrippZ

Hello I'm having some problems with a a sortedList in C#.
My set is as follows: I have a sortedList as a class variable
(DataList) I inisiate it in my constructor with DataList = new
SortedList().

I add other sorted lists to DataList with the following code:
internal void addData(System.Collections.SortedList inList) {

System.Collections.SortedList inNewList = new SortedList();
try
{
string inListName = (string)inList["owner"];
if(!DataList.ContainsKey(inListName))
{
for(int i=0; i<inList.Count; i++)
{
string key = (string)inList.GetKey(i);
object val = (object)inList.GetByIndex(i);
inNewList.Add(key,val);
}

DataList.Add(inListName, inNewList);

}
else
{
DataList.Remove(inListName);
for(int i=0; i<inList.Count; i++)
{
string key = (string)inList.GetKey(i);
object val = (object)inList.GetByIndex(i);
inNewList.Add(key,val);
}

DataList.Add(inListName, inNewList);
}

}
catch(Exception e)
{
Console.WriteLine("error in DataList");
ErrorLogger.ErrorLogClass.WriteErrorLog(e.ToString());
}
}

The problem I have is when I have added the 9th sortedlist to DataList,
then the DataList is cleared somehow and ends up with Count = 0;
Thefunny thing is that all works perfect until a add the 9th
sortedlist.

I don't have clue to what's wrong here :)
Cheers
//KrippZ
 
J

Jon Skeet [C# MVP]

KrippZ said:
Hello I'm having some problems with a a sortedList in C#.
My set is as follows: I have a sortedList as a class variable
(DataList) I inisiate it in my constructor with DataList = new
SortedList().

I add other sorted lists to DataList with the following code:
internal void addData(System.Collections.SortedList inList)

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

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

Jon
 

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