|
-
Oct 26th, 2006, 10:57 PM
#1
Thread Starter
Fanatic Member
Help with Generic LIst
hello dudes! i have a generic list and I add value to it using the code below
Code:
public List<string> lstMainAlarm = new List<string>();
lstMainAlarm.Add(Alarm_Id);
but i can't figure out how to check is X value exist I already use .Exist but i can't figure out how to use it. what is predicate? any inputs please..
-
Oct 26th, 2006, 11:20 PM
#2
Re: Help with Generic LIst
Code:
List<string> list = new List<string>();
list.Add("Hello World");
if (list.Contains("Hello World"))
{
MessageBox.Show("List says Hello");
}
else
{
MessageBox.Show("List doesn't say Hello");
}
if (list.Contains("Goodbye Cruel World"))
{
MessageBox.Show("List says Goodbye");
}
else
{
MessageBox.Show("List doesn't say Goodbye");
}
Exists() is more complex. Predicate is a delegate. If you want to know more then read the help topics for the Exists method and the Predicate delegate. It's all there in the MSDN library, including code examples.
-
Oct 27th, 2006, 01:30 AM
#3
Thread Starter
Fanatic Member
Re: Help with Generic LIst
I have read about the Contains Methods in my MSDN but i have not found an answer on how it perform its searching whether it search for the item exactly as the paramater you provide or search for an item that has something like the parameter. for example, i have 3 items of type string in my list named my_list. "Hello World","HI ALL","ITS GREAT TO BE HERE" does
Code:
my_list.Contains("Hello");
will return true since of the items contains "Hello"?
-
Oct 27th, 2006, 02:05 AM
#4
Re: Help with Generic LIst
The Contains method tells you whether the collection contains the exact item that you specify. If you want to perfrom a fuzzier search then the Exists method is the way to go, but your first post didn't really indicate that that was what you wanted. When you call Exists you must pass a Predicate, which basically means a method reference with the same signature as the Predicate delegate. The Predicate delegate has a return type of 'bool' and has a single argument of type T, which means the same type as the items in your List. If you're using a List<string> then the method you're calling must return a bool and take a single string parameter. That method is then called once for each item in the List, with the item being passed to the method argument. If the method returns False every time then Exists returns False. If the method returns True once then it is not called for any more items. Try this code as an example:
Code:
private string substring;
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Hello World");
list.Add("Hi There");
list.Add("Goodbye Cruel World");
this.substring = "world";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'world'");
}
else
{
MessageBox.Show("List does not contain 'world'");
}
this.substring = "goodbye";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'goodbye'");
}
else
{
MessageBox.Show("List does not contain 'goodbye'");
}
this.substring = "pineapple";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'pineapple'");
}
else
{
MessageBox.Show("List does not contain 'pineapple'");
}
}
private bool ContainsSubstring(string item)
{
MessageBox.Show("Item: " + item + "; Substring: " + this.substring);
// Check whether the current item contains the specified substring.
return (item.IndexOf(this.substring, StringComparison.CurrentCultureIgnoreCase) != -1);
}
-
Oct 27th, 2006, 02:08 AM
#5
Re: Help with Generic LIst
Note that to show where the Predicate delegate comes in, this:
Code:
if (list.Exists(ContainsSubstring))
is actually shorthand for this:
Code:
if (list.Exists(new Predicate<string>(ContainsSubstring)))
-
Oct 27th, 2006, 06:41 PM
#6
Thread Starter
Fanatic Member
Re: Help with Generic LIst
Thanks for the example Jm..it really gives me an idea what a predicate is and how to use it for List..i'll try that as soon as i get to work..Thanks again.
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
|