Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

List.FindLastIndex with parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sam - 06 Aug 2007 21:54 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.