By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

How to pass a parameter to a predicate delegate?

P: n/a
I have seen examples for List<T>.FindAll(findthis)where findthis is a
predicate. How do I pass a parameter to this predicate so that I have
different values to search for? I don't want to use global variables.
Sep 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John Dalberg <no****@nospam.ssswrote:
I have seen examples for List<T>.FindAll(findthis)where findthis is a
predicate. How do I pass a parameter to this predicate so that I have
different values to search for? I don't want to use global variables.
Use a captured variable. For instance:

public void FindByName(List<Personpeople, string name)
{
return people.FindAll(delegate (Person person)
{ return person.Name==name; }
);
}

The "name" parameter is captured by the delegate.

See http://pobox.com/~skeet/csharp/csharp2/delegates.html for more
information.

--
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
Sep 1 '07 #2

P: n/a
John Dalberg wrote:
I have seen examples for List<T>.FindAll(findthis)where findthis is a
predicate. How do I pass a parameter to this predicate so that I have
different values to search for? I don't want to use global variables.
You can't use global variables. C# doesn't have global variables.

There have been a number of threads in this newsgroup regarding this and
similar questions. However, the short answer:

Using an anonymous method, you can simply include the necessary
variables in the code explicitly. IMHO this is the best mechanism
(lamba expressions notwithstanding :) ).

Otherwise, you need to put the data into the class instance in which the
delegate method you're using is defined, or at least into a class that's
accessible by that method. A common technique is to define a small
class containing just the data and the delegate method, instantiate the
class and use that instance's method as the delegate.

If the above doesn't answer your question, I recommend a Google Groups
search using the keywords "delegate" and "predicate", and possibly other
keywords like "parameter" and "variable".

Pete
Sep 1 '07 #3

P: n/a
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
John Dalberg wrote:
I have seen examples for List<T>.FindAll(findthis)where findthis is a
predicate. How do I pass a parameter to this predicate so that I have
different values to search for? I don't want to use global variables.

You can't use global variables. C# doesn't have global variables.

There have been a number of threads in this newsgroup regarding this and
similar questions. However, the short answer:

Using an anonymous method, you can simply include the necessary
variables in the code explicitly. IMHO this is the best mechanism
(lamba expressions notwithstanding :) ).

Otherwise, you need to put the data into the class instance in which the
delegate method you're using is defined, or at least into a class that's
accessible by that method. A common technique is to define a small
class containing just the data and the delegate method, instantiate the
class and use that instance's method as the delegate.

If the above doesn't answer your question, I recommend a Google Groups
search using the keywords "delegate" and "predicate", and possibly other
keywords like "parameter" and "variable".
Thanks.

By global I meant variables defined in the same class. You can use
variables defined in other clases which serve as global variables.
Sep 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.