Solved

Get values from SortedList

Posted on 2003-12-10
6
741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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