searching an index with FindIndex() method
-
Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!
-
Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!
MSDN topic for List.FindIndex Method[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!
The MSDN information is what I would read to get the information, as well as some sample code, though I do have some trouble understaing it sometimes. From my understanding (still learning) the FindIndex takes a predicate. The predicate is basically a function that accepts a value of whatever type your list contains, does a calculation, and returns true or false. So let's say I have a List of INTs. If I wanted to search for the first occurance of a number greater than 5, I would create a function like this:
public bool IsGreaterThan5(int i) { if(i > 5) return true; else return false; }
Then, when I want to find the first instance in my INT list that is greater than 5, I do this.int FirstOccurance = myIntList.FindIndex(IsGreaterThan5);
There are also other variations of the FindIndex function that you can use: List.FindIndex(int32, predicate) - Limits the search range from the int32 element to the end of the list List.FindIndex(int32, int32, predicate) - Limits the search range from int32 to int32. Hope that helps! -
The MSDN information is what I would read to get the information, as well as some sample code, though I do have some trouble understaing it sometimes. From my understanding (still learning) the FindIndex takes a predicate. The predicate is basically a function that accepts a value of whatever type your list contains, does a calculation, and returns true or false. So let's say I have a List of INTs. If I wanted to search for the first occurance of a number greater than 5, I would create a function like this:
public bool IsGreaterThan5(int i) { if(i > 5) return true; else return false; }
Then, when I want to find the first instance in my INT list that is greater than 5, I do this.int FirstOccurance = myIntList.FindIndex(IsGreaterThan5);
There are also other variations of the FindIndex function that you can use: List.FindIndex(int32, predicate) - Limits the search range from the int32 element to the end of the list List.FindIndex(int32, int32, predicate) - Limits the search range from int32 to int32. Hope that helps!Hi guys, I've read the msdn library for both code sample of this method and the documentation about it. But i didn't understand it very well. I've also searched with google for another example... This is what i've tried: // convertedString is an int int indexOfNumber = this.listViewItems.FindIndex(FindIndexOfListViewItem(convertedString); private int FindIndexOfListViewItem(int index) { for (int i = 0; i < this.listViewItems.Count; i++) { string currentItem = this.listViewItems[i].ToString(); //this.number is the item i wanna add as a subItem if (currentItem == this.number) { return i; } } return -1; } These are the errors i get: The best overloaded method match for 'System.Collections.Generic.List.FindIndex(System.Predicate)' has some invalid arguments Argument '1': cannot convert from 'int' to 'System.Predicate' What am i doing wrong? Thanks in advance! -- modified at 3:26 Thursday 11th January, 2007