How to pass a parameter to a predicate delegate?

  • Thread starter Thread starter John Dalberg
  • Start date Start date
J

John Dalberg

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.
 
John Dalberg said:
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<Person> people, 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.
 
John said:
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
 
Peter Duniho said:
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.
 
Back
Top