473,699 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with List<>..Find()

I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
Jun 27 '08 #1
6 2683
On May 7, 9:17*am, Phil <phi...@sbcglob al.netwrote:
I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. *What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
One way is to use the .Contains method, but it will only find the
first one. There is also .FindExactStrin g method if the list is a list
of strings.
Jun 27 '08 #2
On May 7, 9:17 am, Phil <phi...@sbcglob al.netwrote:
I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
int b = items.Find(dele gate(int a) { return a == val; });
Jun 27 '08 #3
On May 7, 2:26 pm, parez <psaw...@gmail. comwrote:
int b = items.Find(dele gate(int a) { return a == val; });
Or in C# 3:

int b = items.Find(a =a==val);

Jon
Jun 27 '08 #4

If you're only concerned with int's..you have an answer already.

If you're interested in this .. in general..I'd suggest:
http://ludwig-stuyck.spaces.l ive.com/Lists/cns!E36D9BA98FC 913B3!296/

Find the C# Generics article (in english) and bookmark and read that.

...

"Phil" <ph****@sbcglob al.netwrote in message
news:B1******** *************** ***********@mic rosoft.com...
>I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't
understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil

Jun 27 '08 #5
Try:
items.Select(i =i == val);
or
items.Find(i =i == val);
or
items.Where(i =i == val);

depending on what you mean by Find... refer to
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Regards,
Leo

"Phil" <ph****@sbcglob al.netwrote in message
news:B1******** *************** ***********@mic rosoft.com...
>I have seen the samples for Find that explain how to use the predicate, but
they are always searching for a pre-defined value. What I don't
understand
is how to search for a random value stored in a variable.

For example,

List<intitems;
int val=3;

items.Add(1);
items.Add(2);
items.Add(3);
items.Add(4);

How do I search the list "items" for the value in the variable val?

Thanks,

Phil
Jun 27 '08 #6
Phil wrote:
I have seen the samples for Find that explain how to use the
predicate, but they are always searching for a pre-defined value.
What I don't understand is how to search for a random value stored in
a variable.
Are you sure you don't want IndexOf instead of Find?
Jun 27 '08 #7

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

Similar topics

4
2482
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic in the standard, maybe I searched not enough thoroughly?
4
52977
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
9
7890
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
0
1748
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57551
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
44
39167
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
3
3252
by: muquaddim | last post by:
Hello, I have a xml file like the following. <?xml version="1.0"> <data> <idef units="Vin,Vout,E"> <i id="i1"> <sample num="1"> <sampledata value="2;3;7" /> </sample>
11
5006
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
4
8926
by: =?Utf-8?B?SkI=?= | last post by:
Hello List<Tis said to be more powerful than ArrayLists but if you have something like this: List<intmylst = new List<>; myList.Add("Joe"); myList.Add(25); the list doesn't seem to accept the name "Joe".
0
8691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.