Solved

Get values from SortedList

Posted on 2003-12-10
6
744 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 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