473,769 Members | 5,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass a parameter to a predicate delegate?

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
3 7349
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==na me; }
);
}

The "name" parameter is captured by the delegate.

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

--
Jon Skeet - <sk***@pobox.co m>
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
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
Peter Duniho <Np*********@Nn OwSlPiAnMk.comw rote:
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2431
by: Isaac | last post by:
Hello, I'm using the Generic List class in System.Collection.Generics to implement a strongly-typed list of my own class. I want to implement the Find methods and have to use the Predicate delegate to do this. As I understand it: I create a method which returns a bool and takes in an object of type <T> where <T> is the type that the generic list was created on.
4
21760
by: yoramo | last post by:
hello can I pass a static method as a parameter to a method? if the answer is yes how do I do that ? how do I call the method ? yoramo.
5
75281
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a compilation error:
3
5580
by: BG | last post by:
Here's my program. { array<Byte> ^buffer = // something int length = Array::FindIndex(buffer, gcnew Predicate<Byte>(&MyClass::isChar13) ); ... } bool MyClass::isChar13(Byte b)
8
78096
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? Thanks.
2
7172
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is a match. If a match is found, I need to move the matched item into another list. I want to compare the objects by value. The items in each list have an ID property (of type object) which I want to convert to a strng in order to be able to do...
3
7186
by: Bob Speaking | last post by:
Hi at all, Is possible to pass a parameter though a delegate or to override it? (I'm newbie and I'm trying to understand delegates and their use in a real scenario) In my scenario I need to override System.Text.RegularExpression.MatchEvaluator delegate passing it another parameter. For a concrete sample I paste some lines of code : //my regexpression pattern;
6
2847
by: =?Utf-8?B?emlubw==?= | last post by:
I'm trying to pass a delegate as parameter but the code below does not compile. It display an error: 'AddressOf operand must the name of a method(without parantheses)' How can I make it work. Public Class CacheFactory Delegate Function myDelegateReport(ByVal myInfoStore As CrystalDecisions.Enterprise.InfoStore) As System.Collections.Generic.Dictionary(Of Long, String)
24
55232
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
0
9583
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
9423
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
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
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...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5297
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...
1
3955
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
3
2814
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.