-
December 7th, 2006, 03:42 AM
#1
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));
-
December 7th, 2006, 04:00 AM
#2
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.
-
December 7th, 2006, 10:07 AM
#3
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"; }));
-
December 7th, 2006, 02:04 PM
#4
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
-
December 7th, 2006, 03:03 PM
#5
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);
-
December 7th, 2006, 03:34 PM
#6
Re: List<string> myList = new List<string>();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|