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
Jon Skeet [C# MVP] - 06 Aug 2007 22:17 GMT
> I use a List<T> and the method FindLastIndex like this:
>
[quoted text clipped - 17 lines]
>
> private bool TheSameYear(XmlNode n, string MyString)
Use an anonymous method:
int iIndex = ListArticle.FindLastIndex(delegate (XmlNode node)
{ return TheSameYear(node, "a custom parameter"); }
);

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
sam - 07 Aug 2007 21:13 GMT
> 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
>> I use a List<T> and the method FindLastIndex like this:
>>
[quoted text clipped - 24 lines]
> { return TheSameYear(node, "a custom parameter"); }
> );
Marc Bartsch - 06 Aug 2007 22:23 GMT
Hi Sam,
sam schrieb:
> I use a List<T> and the method FindLastIndex like this:
>
[quoted text clipped - 19 lines]
>
> 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.
Göran Andersson - 06 Aug 2007 22:44 GMT
> I use a List<T> and the method FindLastIndex like this:
>
[quoted text clipped - 19 lines]
>
> 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);

Signature
Göran Andersson
_____
http://www.guffa.com