List.FindLastIndex with parameter

S

sam

I use a List<T> and the method FindLastIndex like this:

List<XmlNode> ListArticle;
int iIndex = ListArticle.FindLastIndex(TheSameYear);
....
private bool TheSameYear(XmlNode n)
{
if(....)
return true;
else
return false;
}

I need to add a parameter to the callback method, but the prototype
refused add some custom value.

how can I do to pass a value to the callback like this:

int iIndex = ListArticle.FindLastIndex(TheSameYear, "a custom parameter");

private bool TheSameYear(XmlNode n, string MyString)

Sam
 
J

Jon Skeet [C# MVP]

sam said:
I use a List<T> and the method FindLastIndex like this:

List<XmlNode> ListArticle;
int iIndex = ListArticle.FindLastIndex(TheSameYear);
...
private bool TheSameYear(XmlNode n)
{
if(....)
return true;
else
return false;
}

I need to add a parameter to the callback method, but the prototype
refused add some custom value.

how can I do to pass a value to the callback like this:

int iIndex = ListArticle.FindLastIndex(TheSameYear, "a custom parameter");

private bool TheSameYear(XmlNode n, string MyString)

Use an anonymous method:

int iIndex = ListArticle.FindLastIndex(delegate (XmlNode node)
{ return TheSameYear(node, "a custom parameter"); }
);
 
M

Marc Bartsch

Hi Sam,
I use a List<T> and the method FindLastIndex like this:

List<XmlNode> ListArticle;
int iIndex = ListArticle.FindLastIndex(TheSameYear);
...
private bool TheSameYear(XmlNode n)
{
if(....)
return true;
else
return false;
}

I need to add a parameter to the callback method, but the prototype
refused add some custom value.

how can I do to pass a value to the callback like this:

int iIndex = ListArticle.FindLastIndex(TheSameYear, "a custom parameter");

private bool TheSameYear(XmlNode n, string MyString)

Sam
What you can do is use a inner predicate class that stores your custom parameter, like:

List<XmlNode> ListArticle;
SameYearPredicate theSameYear = new SameYearPredicate("a custom parameter");
int iIndex = ListArticle.FindLastIndex(theSameYear.Eval);

....
class SameYearPredicate
{
private string param;

public SameYearPredicate(string param)
{
this.param = param;
}

public bool Eval(XmlNode n)
{
// use this.param here
if(....)
return true;
else
return false;
}
}

Best wishes,

Marc.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

sam said:
I use a List<T> and the method FindLastIndex like this:

List<XmlNode> ListArticle;
int iIndex = ListArticle.FindLastIndex(TheSameYear);
...
private bool TheSameYear(XmlNode n)
{
if(....)
return true;
else
return false;
}

I need to add a parameter to the callback method, but the prototype
refused add some custom value.

how can I do to pass a value to the callback like this:

int iIndex = ListArticle.FindLastIndex(TheSameYear, "a custom parameter");

private bool TheSameYear(XmlNode n, string MyString)

Sam

Put the method in a class, add a private variable in the class, and send
the parameter when creating an instance of the class. The method can use
the local variable.

And please don't use the "if x then true else false" antipattern. ;)

public class YearComparer {

private string _custom;

public YearComparer(string custom) {
_custom = custom;
}

public bool TheSameYear(XmlNode node) {
return ( ...blahblah... _custom ...blahblah.... );
}

}

YearComparer comparer = new YearComparer("a custom parameter");
int iIndex = ListArticle.FindLastIndex(comparer.TheSameYear);
 
S

sam

Jon Skeet said:
Use an anonymous method:

int iIndex = ListArticle.FindLastIndex(delegate (XmlNode node)
{ return TheSameYear(node, "a custom parameter"); }
);
Woaaaaahh very powerfull !!!
Thank you very much it works perfectly and open my mind about this technic
!!

sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top