Solved

Get values from SortedList

Posted on 2003-12-10
6
732 Views
Last Modified: 2012-08-13
I have a sorted list containing "IndexEntry" objects (my custom class) as values. I loop through this list in a foreach block, like this:

SortedList list = new SortedList();
...
int counter = 0;
foreach (DictionaryEntry d in list)
{
    ...
    counter++;
}

In this block, I have another loop, that should start at the counter position + 1 in the SortedList, and break at a certain criteria:

for (int i = counter + 1; i < list.Count; i++)
{
    ...
}

Here's the problem: in this inner loop, I need the current value (the IndexEntry object) in list[i], and I don't know the syntax for getting this object. I tried for example:

IndexEntry e = (IndexEntry)list[i];
IndexEntry e = (IndexEntry)list.Values[i];

Hope this is a good enough explanation...
0
Comment
Question by:liljegren
6 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9915144
IndexEntry e = (IndexEntry)list[i]; should work pretty well. The only important thing is that you have really an IndexEntry element there, not another type of object.

Why don't you make your loops somewhat easier like this:

SortedList list = new SortedList();
...
for (int counter = 0; counter < list.Count; counter++) {
  IndexEntry e1 = (IndexEntry)list[counter]; // If you need the value at possition counter
  ...
  for (int i = counter + 1; i < list.Count; i++) {
    IndexEntry e2 = (IndexEntry)list[i];
    ...
  }
  ...
}
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9915149
let me see if I understood

SortedList list = new SortedList();

// Code

int counter = 0;

foreach (DictionaryEntry d in list)
{
   for( int i = counter + 1; i < list.Count; i++)
   {
        //Set break condition
   }
    counter++;
}


If I didn't understood you wrong, thats more or less what you want to get

I just used your example and I guess it works, I dont see the error

If I were you, in order to keep it simple I would do like this.......


foreach (DictionaryEntry d in list)
{
   for( int i = counter + 1; i < list.Count; i++)
   {
        IndexEntry e = (IndexEntry)list[i];
   }
    counter++;
}


Another way to do it is ...........

SortedList list = new SortedList();

// Code

for( int counter = 0; counter < list.Count; counter ++)
{
   for( int i = counter + 1; i < list.Count; i++)
   {
        IndexEntry e = (IndexEntry)list[i];
   }
    counter++;
}


Hope this helps

TIncho
0
 

Author Comment

by:liljegren
ID: 9915837
I already got the answer, but I still get an error when I write:

IndexEntry e = (IndexEntry)list[i];

I double the points, if you help me further. :-)

Here's the error:

Unhandled Exception: System.InvalidOperationException: Specified IComparer threw
 an exception. ---> System.ArgumentException: Object must be of type String.

Obviously (IndexEntry)list[i] is not returning an IndexEntry object. Here's how the SortedList gets filled:

foreach (IndexEntry e1 in arrayList)
{
    if (e1.Context != "")
    {
        list.Add(e1.Context + "/" + e1.Text, e1);
    }
    else
    {
        list.Add(e1.Text, e1);
    }
}

So, first a string consisting of properties from the object, then the whole object itself. Is there a problem here? Could it be that (IndexEntry)list[i] is returning the key and not the value? If so, how do I get the value?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9916254


Your problem is right here:

 for( int i = counter + 1; i < list.Count; i++)
   {
        IndexEntry e = (IndexEntry)list[i];
   }



"list" is a SortedList object whose template looks like:

SortedList[object key]



Hence, you are using an integer when you should be using an object as a key.

If you *really* want to use an integer, then you need to box it as an object



 for( int i = counter + 1; i < list.Count; i++)
   {
        IndexEntry e = (IndexEntry)list[(object)i];
   }


but that of course assumes that the objects stored in your sortedlist used integers as keys in the first place.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 9916336
Just replace everywhere list[counter] and list[i] with list.GetByIndex (counter) and list.GetByIndex(i) and everything is fine
0
 

Author Comment

by:liljegren
ID: 9916472
Thank you very much everyone. Problem solved.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now