472,789 Members | 1,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

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
5 73712
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Young | last post by:
Hi I have just started messing about with generics. I have done this.. List<string> myList = new List<string>(); I've added strings to the list... But now I want to clear the list...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
8
by: Gee | last post by:
I get the above error with this code and I can't figure out why? Any ideas please? See code below - the actual error is included in the code: public struct NETRESOURCE { public Int32...
7
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string,...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
1
by: hanna88 | last post by:
what's the best way to convert a list of string to a vector<int>? what i have now is for loop and iterate each char in the string string base=""; //i have vector<int>row,vector <...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.