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));
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.
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"; }));
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
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);
Re: List<string> myList = new List<string>();