467,923 Members | 1,258 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Cannot convert from 'string' to 'System.Predicate<string>

Any easy answer what is wrong here?

private List<string> BodyWords = new List<string>();

string word = "Andrew";

the following causes a compilation error:

if (!BodyWords.Exists(word))

{

}

Thanks,
Dec 20 '05 #1
  • viewed: 61471
Share:
5 Replies
Alright, looks like I should be using 'Contains' instead of 'Exists' but I
would still like to understand the difference and what a Predicate is?

Thanks,

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:O0**************@TK2MSFTNGP15.phx.gbl...
Any easy answer what is wrong here?

private List<string> BodyWords = new List<string>();

string word = "Andrew";

the following causes a compilation error:

if (!BodyWords.Exists(word))

{

}

Thanks,

Dec 20 '05 #2
Andrew Robinson <ne****@nospam.nospam> wrote:
Any easy answer what is wrong here?

private List<string> BodyWords = new List<string>();
string word = "Andrew";

the following causes a compilation error:

if (!BodyWords.Exists(word))

{

}


Well, the easy answer is because the List<T>.Exists method doesn't take
a T, it takes a Predicate<T>. I suspect you actually want
List<T>.Contains instead.

--
Jon Skeet - <sk***@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
Dec 20 '05 #3
Hi Andrew,
your problem is that the Exists method expects a Predicate<T> which is
another way of saying you need to put a delegate that returns a bool and
accepts a type of T i.e. delegate bool Predicate<T>(T obj)

So if you want to search your list to see if it contains a value of "Andrew"
you will need to create a method that matches the delegate signature (i.e.
one that returns a bool and accepts the type you define your list as being
(in your case string)

i.e.
private bool ContainsAndrew(string val)
{
return val == "Andrew";
}

//now to your list you would say:

List<string> BodyWords = new List<string>();
BodyWords.Add("Peter");
BodyWords.Add("Paul");

bool containsAndrew = BodyWords.Exists(ContainsAndrew);

The list will then iterate through all the items in the list, calling the
ContainsAndrew method against each item until a match is found (if there is a
match).

Hope that helps
Mark Dawson
http://www.markdawson.org

"Andrew Robinson" wrote:
Any easy answer what is wrong here?

private List<string> BodyWords = new List<string>();

string word = "Andrew";

the following causes a compilation error:

if (!BodyWords.Exists(word))

{

}

Thanks,

Dec 20 '05 #4
Alright, looks like I should be using 'Contains' instead of 'Exists' but I
would still like to understand the difference and what a Predicate is?


Predicate is simply a generic delegate with the signature

delegate bool Predicate<T> (T val)

and you're supposed to supply such an instance to Exists. It will
return true if the predicate returns true for any item. So you can do
something like

if (!BodyWords.Exists(delegate (string s) { return s == word; }))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 20 '05 #5
Andrew Robinson <ne****@nospam.nospam> wrote:
Alright, looks like I should be using 'Contains' instead of 'Exists' but I
would still like to understand the difference and what a Predicate is?


A Predicate is something which returns true or false when given an item
of the appropriate type. For instance, here's a Predicate<string> which
tests for a string having a length 5 or more:

Predicate<string> lengthTester = delegate(string x)
{ return x.Length >= 5; };

(That's using an anonymous method, but a Predicate<T> is just like any
other delegate.)

--
Jon Skeet - <sk***@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
Dec 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John Young | last post: by
5 posts views Thread by Gary Wessle | last post: by
5 posts views Thread by David Longnecker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.