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.Collecti ons.Generic;
using System.Componen tModel;
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) };
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
Point first = new Point(11,11);
try
{
first = Array.Find(poin ts, ProductGT10);
MessageBox.Show ("Point is: " + first.X.ToStrin g());
}
catch (ArgumentNullEx ception)
{
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(poin ts, ProductGT10_2(s econd, R2));
/*
//error CS0411: The type arguments for method
'System.Array.F ind<T>(T[], System.Predicat e<T>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly
*/
MessageBox.Show ("Point2 is: " + second.X.ToStri ng());
}
catch (ArgumentNullEx ception)
{
MessageBox.Show ("null2");
}
}
///////////////////
private static bool ProductGT10(Poi nt 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(P oint P, Rectangle R) //
passing the R
{
if (R.Contains(P))
{
return true;
}
else
{
return false;
}
}
}//end of Form1
} //end of Forms01 namespace 3 6125
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(poin ts, ProductGT10_2(s econd, R2));
/*
//error CS0411: The type arguments for method
'System.Array.F ind<T>(T[], System.Predicat e<T>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly
*/
The Predicate<Tdele gate 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 ProductGT10Comp arer {
private Rectangle _r;
public ProductGT10Comp arer(Rectangle r) {
_r = r;
}
public bool ProductGT10(Poi nt p) {
return _r.Contains(p);
}
}
Create an instance of the class passing the R2 value, and use it's method:
first = Array.Find<Poin t>(points, new ProductGT10Comp arer(R2).Produc tGT10);
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<Poin t>(points, delegate(Point p) { return
R2.Contains(p); });
--
Göran Andersson
_____ http://www.guffa.com
On Aug 22, 12:54*pm, Göran Andersson <gu...@guffa.co mwrote:
>
Create an instance of the class passing the R2 value, and use it's method:
first = Array.Find<Poin t>(points, new ProductGT10Comp arer(R2).Produc tGT10);
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<Poin t>(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
raylopez99 <ra********@yah oo.comwrote:
On Aug 22, 12:54*pm, Göran Andersson <gu...@guffa.co mwrote:
Create an instance of the class passing the R2 value, and use it's method:
first = Array.Find<Poin t>(points, new ProductGT10Comp arer(R2).Produc tGT10);
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<Poin t>(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.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 colors are referred in different subsystems in our projects. They are
defined as enumeration constants and a single color must be the same value
all across our projects.
|
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
|
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 same thing (though I dont
know if this is better), I could convert the array to some other structure
which makes direct lookup possible (if I want to do the same thing many
times), or maybe some other entirely different approach is better. Any
ideas?
|
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 OleDb.OleDbCommand("zsp_web_WebDocument",
cnnSearch)
cmdWebDocGroup.CommandType = CommandType.StoredProcedure
prmWebDocGroup = cmdWebDocGroup.Parameters.Add("@strParm00",
|
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 ObjectDataSource binding to a SQLX db
using a foreign key to filter the returned result set to display in a
GridView.
The error message in the subject line is generated when I try to bind the
following GridView to the ObjectSource that follows...
| |
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 properties (e.g.
..ID, .LastName, .FirstName) I am populating an array of Contacts with an XML
feed, and everything is working great.
I'm at a point where I need to perform a search within my array of Contacts,
and I'm struggling with the Array.Find()...
|
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 want to replace all of them.
You need to look at it in a case-by-case basis. What can you do?
Answer: emacs.
|
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 parameters to the new
thread.
TIA
|
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"
Public Class States
Public Sub New()
End Sub
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |