Yustme

Hi,

I've read the msdn library about this method and a sample code. I didn't quite understand it.

I made an object of List:

List<ListViewItems> numbers = new List<ListViewItems>

I want do add an item as a subItem to the listview control. The item i wanna add should added behind an element that matches the criteria.

Im trying to find the index of the element so i can add it like this:

this.numbers[index].SubItems[2].Text = "Done";

But im having a little problem using the numbers.FindIndex() method.

This is the method im trying to call in the FindIndex() method like this:

number.FindIndex(FindIndexOfListViewItem)

private int FindIndexOfListViewItem(int index)
{
for (int i = 0; i < this.listViewItems.Count; i++)
{
string currentItem = this.listViewItems[ i ].ToString();

if (currentItem == this.number)
{
return i;
}
}

return -1;
}

I've also tried it like this:

int i = 5;

number.FindIndex(FindIndexOfListViewItem(i))

Can somebody tell me what im doing wrong

Thanks in advance!


Re: .NET Base Class Library FindIndex() method

TaylorMichaelL

If you're using FindIndex then you must pass a Predicate delegate to it. The syntax for a predicate is:

bool Predicate<T> (T obj)

Your FindIndexOfListViewItem does not match that signature.

Assuming that you have some complex heuristics to identify an item to return you'd use this syntax:

int nIndex = numbers.FindIndex(FindMyItem);

private bool FindMyItem ( ListViewItem item )
{
if (condition)
return true;

return false;
}

The problem is that you have no way to associate arbitrary data in order to check your conditions. You'll end up needing to create an instance of a custom class that contains the additional information you need. This custom class would then provide the predicate function to do the comparison work. A little more work but not unreasonable.

public class MyCustomPredicate
{
public MyCustomPredicate ( int number )
{ ... }

public bool MeetsCriteria ( ListViewItem item )
{ ... }
}

int nIndex = numbers.FindIndex(new MyCustomPredicate(number).MeetsCriteria);

Note that you can also use the IndexOf method but it would require that you pass it the actual ListViewItem to be found. Alternatively a simple for-each loop would work just as well.

Michael Taylor - 1/11/07
http://p3net.mvps.org





Re: .NET Base Class Library FindIndex() method

Mark Dawson

Hi Michael,

"The problem is that you have no way to associate arbitrary data in order to check your conditions. You'll end up needing to create an instance of a custom class that contains the additional information you need. This custom class would then provide the predicate function to do the comparison work. A little more work but not unreasonable."

The OP can use an anonymous method to define the Predicate method. With anonymous methods you can reference local variables and class fields from within the anonymous delegate declaration. In this way they would not have to create a new class. For example:

using System;

using System.Collections.Generic;

namespace Test

{

class Program

{

static void Main(string[] args)

{

Test t = new Test(2);

t.Run();

}

}

class Test

{

private int number;

public Test(int number)

{

this.number = number;

}

public void Run()

{

List<int> numbers = new List<int>();

numbers.Add(0);

numbers.Add(1);

numbers.Add(2);

numbers.Add(3);

int index = numbers.FindIndex(

delegate(int currentListItem)

{

return currentListItem == this.number;

});

}

}

}

Thanks
Mark.





Re: .NET Base Class Library FindIndex() method

Yustme

Hi guys,

Thank you very much for your reply!

It looks promessing, i'll try it!