CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    22

    Question List<string> myList = new List<string>();

    I want use the metod public T Find (Predicate<T> match) to find an string in myList.
    But I have no idea how can I find a string as a parameter.

    I know only this way:
    List<string> mylist = myList.FindAll(demo);


    Is this way possible:
    List<string> mylist = myList.FindAll(demo(object x));

  2. #2
    Join Date
    Feb 2006
    Posts
    8

    Re: List<string> myList = new List<string>();

    // 'result' will hold all items in your 'yourList' that matches the criteria.
    List<string> result = yourList.FindAll(func);

    // func should look like that:
    bool func(string p)
    {
    // your criteria goes here
    // return true if the string 'p' matches your criteria, false otherwise.
    return ...
    }

    the function 'func' is called by the framework on each item in your list.

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: List<string> myList = new List<string>();

    you can also inline it like this:

    Code:
    List<string> foo = new List<string>();
    List<string> matches = foo.FindAll(new Predicate<string>(delegate(string target) { return target == "something"; }));

  4. #4
    Join Date
    Nov 2006
    Posts
    22

    Question Re: List<string> myList = new List<string>();

    class Demo
    {
    class DemoItem
    {
    string myString;
    DateTime myDate;
    }
    class DemoItems : List<DemoItem>
    {
    private DateTime dtDate;

    public delegate bool CompareDate(Demo.Item x)
    {
    return (x.Date.CompareTo(DateTime.Now)==0);
    }
    }
    }
    ...
    ...
    ...
    Demo.Item di = dis.Find(CompareDate);
    ...
    And now I must write for each Date an own method? ComapreDate1 .... CompareDateX

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: List<string> myList = new List<string>();

    I'd suggest creating a search class that allows you to specify what date you want to comper it to, then use one of its methods to test if the condition is true or not.

    ex:

    Code:
    public class DateComparer {
        DateTime test;
        public DateComparer(DateTime testTime) {
            this.test = testTime;
        }
        public bool IsGreaterThan(DateTime candidate) {
            return candidate > test;
        }
        public bool IsLessThan(DateTime candidate) {
            return candidate < test;
        }
        public bool IsEqualTo(DateTime candidate) {
            return candidate == test;
        }
    }
    then:

    Code:
    List<DateTime> dates = new List<DateTime>();
    DateComparer comparer = new DateComparer(DateTime.Now);
    List<DateTime> one= dates.FindAll(comparer.IsGreaterThan);
    List<DateTime> two = dates.FindAll(comparer.IsLessThan);

  6. #6
    Join Date
    Nov 2006
    Posts
    22

    Thumbs up Re: List<string> myList = new List<string>();

    Thanks. That´s it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured