Solved

Get values from SortedList

Posted on 2003-12-10
6
731 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

13 Experts available now in Live!

Get 1:1 Help Now