473,624 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "FindLastIn dex" 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.

RL

// 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
//OUTPUT:

/*
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++)
{
newList.Add(i);
}
newList.Remove( 9);
//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
-10

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

Console.WriteLi ne("\n");
int y = 5; //the value we want as the comparison point

int lastIndex = newList.FindLas tIndex(value =value >=
y);
int firstIndex = newList.FindInd ex(value =value >= y);

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

Console.WriteLi ne("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.FindLas tIndex(MyPredic ate);
Console.WriteLi ne("lastIndex2 is: {0}", lastIndex2);
int firstIndex2 = newList.Find(My Predicate);
Console.WriteLi ne("firstIndex 2 is: {0}", firstIndex2);

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

int lastIndex3 = newList.FindLas tIndex(new
PredicateClass( y).MyPredicate) ;
Console.WriteLi ne("lastIndex3 is: {0}", lastIndex3);
int firstIndex3 = newList.Find(ne w
PredicateClass( y).MyPredicate) ;
Console.WriteLi ne("firstIndex 3 is: {0}", firstIndex3);

}
private static bool MyPredicate(int value)
{
if (value >= 5) //value “hard coded” here –compare with
other versions
{ return true ; }
else
{ 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;}
else
{return false;}
}

}

Sep 10 '08 #1
0 1413

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

Similar topics

1
3572
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, ... } Employee table has a primary key (combination of last_name and first_name).
2
11255
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 change color when any of the three are hovered over. I would prefer to do this with pure CSS if possible, but if Javascript is the only way...so be it. --
2
1721
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 = ~(p1 ^ p2); // ^ is the AND operator in logic notation
0
1163
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
2
1820
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. Regards, Praveen.
9
2818
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 invalid use of null. Fair enough Field1 and Field2 can contain nulls I altered the 2 queries to exclude nulls from FasText and FasInteger-
1
8894
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 "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
7
1504
balabaster
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 "EndsWithSaurus" demonstration is beyond useless at explaining what this is useful for. Having previously understood delegates and got my head around those, predicates finally makes sense...although I still think Microsoft's example is ridiculous. I...
4
3877
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 variables declared in SP. select * from table where pred1 and pred2 and pred3 and variable1 ='number1 and variable2 =number2;
0
8631
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
8341
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
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
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
2
1489
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.