472,986 Members | 2,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Predicates explained: three ways to declare predicates

Inspired by Jon, I did a demo prorgram showing three ways to declare
predicates, in for example the "FindIndex" and "FindLastIndex" methods
of Lists, but in general you can do this for any predicate.

Note the most compact way is to use Lambda notation and anonymous
functions, the first way shown below. The most 'intuitive' way is to
'hard code' the predicate using a predicate method, the second way
shown below, but the best way (for me) is to use an idea Goran gave me
and that is to construct a predicate class that 'generalizes' the hard
codes value you are using in your predicate. This is shown in the
third way below.

If you study this example you will have mastered predicates, which are
used everywhere, for example in Array.Find as well.


// see also http://msdn.microsoft.com/en-us/libr...bz(VS.80).aspx
(gives a simplistic example, that really does not do justice to the
topic, as is typical for MSDN)

// September 11, 2008

0 1 2 3 4 5 6 7 8 -10

lastIndex is: 8, firstIndex is 5
lastIndex2 is: 8
firstIndex2 is: 5
lastIndex3 is: 8
firstIndex3 is: 5
Press any key to continue . . .


namespace Console1
class Program
static void Main(string[] args)

List<intnewList = new List<int>();

for (int i = 0; i < 10; i++)
//note: commenting out this above line gives: 0 1 2 3 4 5 6 7 8 -10 9
(ten elements) since nothing removed!

newList.Insert(9, -10); //gives output 0 1 2 3 4 5 6 7 8

foreach (int i in newList)
Console.Write(" {0}", i);

int y = 5; //the value we want as the comparison point

int lastIndex = newList.FindLastIndex(value =value >=
int firstIndex = newList.FindIndex(value =value >= y);

//the above way uses anonymous functions and 'lambda' notation and is
very compact as you can tell

Console.WriteLine("lastIndex is: {0}, firstIndex is {1}",
lastIndex, firstIndex); //last index is 8 (i.e. List[8] = 8) first
index is 5 (List[5]= 5)

// now the second 'hard coded' way--note y>=5 is 'hard coded' into the
predicate method

int lastIndex2 = newList.FindLastIndex(MyPredicate);
Console.WriteLine("lastIndex2 is: {0}", lastIndex2);
int firstIndex2 = newList.Find(MyPredicate);
Console.WriteLine("firstIndex2 is: {0}", firstIndex2);

//now the neatest (IMO) way: the predicate class way (verbose but
easy to understand and flexible)

int lastIndex3 = newList.FindLastIndex(new
Console.WriteLine("lastIndex3 is: {0}", lastIndex3);
int firstIndex3 = newList.Find(new
Console.WriteLine("firstIndex3 is: {0}", firstIndex3);

private static bool MyPredicate(int value)
if (value >= 5) //value “hard coded” here –compare with
other versions
{ return true ; }
{ return false; }


//third way: construct a predicate class, so you can pick any y easily

public class PredicateClass
private int _y;
public PredicateClass(int y)
_y = y;
public bool MyPredicate (int value)
if (value >= _y)
{return true;}
{return false;}


Sep 10 '08 #1
0 1366

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

Similar topics

by: jim | last post by:
I have two tables that are related by keys. For instance, Table employee { last_name char(40) not null, first_name char(40) not null, department_name char(40) not null, age int not null, ......
by: Sugapablo | last post by:
Can anyone help me out with some code to change three table cells (<td>) when one is hovered over? I have a calendar grid where each day is made up of three table cells and I want all three to...
by: Thong Nguyen | last post by:
I wrote my own Predicate class for .NET 1.1 which allowed composite predicates using operator overloading... for example: Predicate p1 = {...}; Predicate p2 = {...}; Predicate p1andp2 =...
by: dayzman | last post by:
Hi, M'm writing a program to output the results of transforming logical predicates using deMorgan's. Does anyone know of a library that handles logical predicates? Cheers, Michael
by: Praveen | last post by:
Hi, Hi I am a newbie in DB2 and Mainframe.I would like to know if there is any diffence between the Quantified Predicates: IN , ANY, and SOME. Thank you very much for your valuable time. ...
by: Yitzak | last post by:
Hi spent a few hours on this one wrote a query that joined on results of 2 other queries. Qry3 using Qry1 and Qry2 When I used Qry1.FasText <cstr(Qry2.FasInteger) in the where clause - got...
Death Slaught
by: Death Slaught | last post by:
I will be showing you how to make a very simple but effective three column layout. First we will begin with the HTML, or structure, of our three column layout. <!DOCTYPE html PUBLIC...
by: balabaster | last post by:
Okay, after having bashed my head against this long enough I thought of you guys... I thought having exhausted just about every other .NET concept I would come back to predicates because Microsoft's...
by: Arun Srinivasan | last post by:
Hi I was using a query previously, that was efficient select * from table where pred1 and pred2 and pred3; Later I was asked to introduce new ones, but they were not based on table columns but...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.