473,416 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

System.Predicate for the System.Collections.Generic.List<string>Exists function

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 generic list...is not clear to me. Any
suggestions?

System.Collections.Generic.List<string> list = new
System.Collections.Generic.List<string>();

foreach(string s in myAnotherArray)
{

System.Predicate<string> p = new System.Predicate<string> (pred);
if(!list.Exists(p))
{
list.Add(s);
}
}
protected bool pred(string target)
{
//TODO: How to find out target string exists in the generic
list????
}

Nov 17 '05 #1
3 15114
Abhi,

Which list are you trying to check to see if the value exists?
Basically, you want to pass the predicate to the list in order to see if the
values that are already in the list meet a certain condition.

You might want to use anonymous delegates here, as they will allow you
to specify the condition much easier depending on what you have set up.

Can you give a code sample, as well as a more detailed explaination of
what the condition you are trying to check is?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Abhi" <ku******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 generic list...is not clear to me. Any
suggestions?

System.Collections.Generic.List<string> list = new
System.Collections.Generic.List<string>();

foreach(string s in myAnotherArray)
{

System.Predicate<string> p = new System.Predicate<string> (pred);
if(!list.Exists(p))
{
list.Add(s);
}
}
protected bool pred(string target)
{
//TODO: How to find out target string exists in the generic
list????
}

Nov 17 '05 #2
you do something like this

foreach( string s in list2 )
{

bool found = list.Exists( delegate(string t) {
return s == t;
} );

if( !found) list.Add( s );

}

"Abhi" wrote:
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 generic list...is not clear to me. Any
suggestions?

System.Collections.Generic.List<string> list = new
System.Collections.Generic.List<string>();

foreach(string s in myAnotherArray)
{

System.Predicate<string> p = new System.Predicate<string> (pred);
if(!list.Exists(p))
{
list.Add(s);
}
}
protected bool pred(string target)
{
//TODO: How to find out target string exists in the generic
list????
}

Nov 17 '05 #3
Hello back Nicholas-

I actually needed to use the "Contains" function ...but due to my
ignorance, I mistook the "Exists" function for that job.

list.Contains("mystring") is what I needed for checking to see if a
string exists in my generic list. Pardon me for the confusion in the
code snippet in my original posting. It was a lame attempt to translate
my original issue into a more "simplistic" representation of the
problem at hand.
Thanks!

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
5
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a compilation error:
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
3
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.