473,231 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Possible to make Array.Find Predicate match method more universal? Totake multiple parameters?

I suspect the answer to this question is that it's impossible, but how
do I make the below code work, at the point where it breaks (marked
below). See error CS0411

This is the complete code. Copy and paste it into VS2008 for a
Windows Forms application.

With some effort I can do the job with a user defined function, but my
question is whether I can do it using the built in library function
"Array.Find" and a predicate that takes multiple parameters rather
than one.

Thanks

RL

//////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Forms01
{
public partial class Form1 : Form
{

Point[] points;

public Form1()
{
points = new Point[]{ new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{

Point first = new Point(11,11);

try
{
first = Array.Find(points, ProductGT10);
MessageBox.Show("Point is: " + first.X.ToString());
}
catch (ArgumentNullException)
{
MessageBox.Show("null");
}

// this first try block works, as expected, when commenting out the
second try block below,
// to give output ‘Point is: 150’
Rectangle R2 = new Rectangle(100, 100, 200, 200);
Point second = new Point(12, 12);

//second try block does not work (does not compile)
try
{
first = Array.Find(points, ProductGT10_2(second, R2));

/*
//error CS0411: The type arguments for method
'System.Array.Find<T>(T[], System.Predicate<T>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly

*/

MessageBox.Show("Point2 is: " + second.X.ToString());
}
catch (ArgumentNullException)
{
MessageBox.Show("null2");
}

}
///////////////////
private static bool ProductGT10(Point p)
{

Rectangle R = new Rectangle(101, 101, 200, 200);

//hard code the rectangle R
//can we avoid having to hard code the rectangle here? Can we pass
it? See below

if (R.Contains(p))
{
return true;
}
else
{
return false;
}
}

//this doesn't compile
private static bool ProductGT10_2(Point P, Rectangle R) //
passing the R
{
if (R.Contains(P))
{
return true;
}
else
{
return false;
}
}
}//end of Form1

} //end of Forms01 namespace

Aug 22 '08 #1
3 6081
raylopez99 wrote:
I suspect the answer to this question is that it's impossible, but how
do I make the below code work, at the point where it breaks (marked
below). See error CS0411

This is the complete code. Copy and paste it into VS2008 for a
Windows Forms application.

With some effort I can do the job with a user defined function, but my
question is whether I can do it using the built in library function
"Array.Find" and a predicate that takes multiple parameters rather
than one.
8< snip
>
Rectangle R2 = new Rectangle(100, 100, 200, 200);
Point second = new Point(12, 12);

//second try block does not work (does not compile)
try
{
first = Array.Find(points, ProductGT10_2(second, R2));

/*
//error CS0411: The type arguments for method
'System.Array.Find<T>(T[], System.Predicate<T>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly

*/
The Predicate<Tdelegate only takes a single parameter, so you can't
pass a method that takes two parameters.

You can create a class for the method, with a member variable for the
rectangle:

public class ProductGT10Comparer {

private Rectangle _r;

public ProductGT10Comparer(Rectangle r) {
_r = r;
}

public bool ProductGT10(Point p) {
return _r.Contains(p);
}

}

Create an instance of the class passing the R2 value, and use it's method:

first = Array.Find<Point>(points, new ProductGT10Comparer(R2).ProductGT10);
Another alternative is to use an anonymous method. Then the delegate
will contain the value of R2 so that the code can use it (so it's pretty
much the same as storing the R2 value in a class instance):

first = Array.Find<Point>(points, delegate(Point p) { return
R2.Contains(p); });

--
Göran Andersson
_____
http://www.guffa.com
Aug 22 '08 #2
On Aug 22, 12:54*pm, Göran Andersson <gu...@guffa.comwrote:
>
Create an instance of the class passing the R2 value, and use it's method:

first = Array.Find<Point>(points, new ProductGT10Comparer(R2).ProductGT10);

Another alternative is to use an anonymous method. Then the delegate
will contain the value of R2 so that the code can use it (so it's pretty
much the same as storing the R2 value in a class instance):

first = Array.Find<Point>(points, delegate(Point p) { return
R2.Contains(p); });
Thanks Goran! I understood the first example, but not the 'anonymous
delegate' example (I'm not used to that format, but it looks
compact). In any event, I appreciate your answer, it's a nice trick
that I'll store in my library, and I'll report back if I have any
problems.

Sincerely,
RL
Aug 22 '08 #3
raylopez99 <ra********@yahoo.comwrote:
On Aug 22, 12:54*pm, Göran Andersson <gu...@guffa.comwrote:

Create an instance of the class passing the R2 value, and use it's method:

first = Array.Find<Point>(points, new ProductGT10Comparer(R2).ProductGT10);

Another alternative is to use an anonymous method. Then the delegate
will contain the value of R2 so that the code can use it (so it's pretty
much the same as storing the R2 value in a class instance):

first = Array.Find<Point>(points, delegate(Point p) { return
R2.Contains(p); });
Thanks Goran! I understood the first example, but not the 'anonymous
delegate' example (I'm not used to that format, but it looks
compact). In any event, I appreciate your answer, it's a nice trick
that I'll store in my library, and I'll report back if I have any
problems.
This is basically the point behind closures - they allow you to carry
around some of your environment (including local variables) within
delegates. See
http://csharpindepth.com/Articles/Ch.../Closures.aspx for other
examples.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 29 '08 #4

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

Similar topics

1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
6
by: Bore Biko | last post by:
I don't know hopw to do this, like "printf", or "sprintf", I know they use a stack but how to make my own...?? Thanks in advance !! robert.bralic@si.t-com.hr
6
by: JezB | last post by:
What is the most efficient way to scan an array for a match ? I could just iterate directly through it comparing each array entry with what I am looking for, I could use an enumerator to do the...
1
by: Steven K | last post by:
Hello, I am calling a SQL Server 2K parameter query with the following: Dim spWebDocGroup As OleDb.OleDbDataReader Dim prmWebDocGroup As OleDbParameter Dim cmdWebDocGroup As New...
1
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and...
4
by: John Wolff | last post by:
Pardon me for such a newbie question. I've only been using C# for 14 months, and most of the stuff has been relatively basic. I have a serializable "Contact" class that has a number of...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
5
by: Code Monkey | last post by:
Is there anyway I can start a get a job done by starting a new thread with MULTIPLE parameters? operations only take a maximum of 1 parameter? I need a way of passing (at minimum) at least 3...
0
by: Cirene | last post by:
Using Visual Studio I created a DataSet using the GUI (XSD file). Trying to use a tiered methodology I called the functions from my BLL. Namespace Zzz.BusinessLogicLayer #Region "DAL Access"...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.