Help with List<>..Find()

P

Phil

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<int> items;
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
 
Z

zacks

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<int> items;
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 .FindExactString method if the list is a list
of strings.
 
P

parez

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<int> items;
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(delegate(int a) { return a == val; });
 
B

Ben Voigt [C++ MVP]

Phil said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top