Solved

Get values from SortedList

Posted on 2003-12-10
6
735 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read json value 2 49
Reading the Web.Config using IIS 7.5? 4 36
application restarter  - for an exe that hangs 9 26
Winform Module - What is the ASP.Net equiv 2 20
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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