I have 3 static methods that share almost the same code with only a few
lines where different operators are used.
Is there a way to pass in an operator as a parameter?
pseudo code
Greater() {Base(>)}
Less() {Base(<)}
Equal() {Base(==)}
Base(operator op)
{
....
if(a op b)
....
}
Or another way of doing this?
Tem 6 7615
Or another way of doing this?
Depending on what you are doing, you could use a delegate - i.e. a
Func<T,T,bool>; whether this is a good idea (or not) depends on the
scenario... easy enough in C# 3 with lambdas...
public static void Greater() {Base((x,y) =x y);}
public static void Less() {Base((x,y) =x < y);}
public static void Equal() {Base((x,y) =x == y);}
private static void Base(Func<Foo,F oo,boolop)
{
//...
if(op(a,b))
{
//...
}
}
(again: I stress hard to give a great answer without more info)
Marc
I should also add that for equaility (==, !=) an inequality (<, <=, >,
>=) you can also use EqualityCompare r<T>.Default and
Comparer<T>.Def ault to do a lot, without requiring direct access to
operators (actually it uses IComparable<Tan d IEquatable<T>).
Marc
"Tem" <te*****@yahoo. comwrote:
I have 3 static methods that share almost the same code with only a
few lines where different operators are used. Is there a way to pass
in an operator as a parameter?
What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate (int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };
bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.
Thank you guys for the help. Now I see what delegates are useful for.
"Paul E Collins" <fi************ ******@CL4.orgw rote in message
news:l5******** *************** *******@bt.com. ..
"Tem" <te*****@yahoo. comwrote:
>I have 3 static methods that share almost the same code with only a few lines where different operators are used. Is there a way to pass in an operator as a parameter?
What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate (int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };
bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.
Can this be written with lambda expressions?
"Paul E Collins" <fi************ ******@CL4.orgw rote in message
news:l5******** *************** *******@bt.com. ..
"Tem" <te*****@yahoo. comwrote:
>I have 3 static methods that share almost the same code with only a few lines where different operators are used. Is there a way to pass in an operator as a parameter?
What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate (int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };
bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.
Can this be written with lambda expressions?
yes; I aleady posted a variant using lambdas (although using the
"delegate" version of a lambda*); using an actual Expression is
possible - not sure if it gains much, though (the delegate is likely
to be quicker than reflection from a PropertyInfo).
Whether this is the *best* approach depends largely on the full
scenario.
*=lambdas can be compiled to either a delegate or a System.Expressi on.
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray;
DoStuff(myArray);
|
by: Victor Bazarov |
last post by:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is
AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing 'retval'
return retval;
}
|
by: paul |
last post by:
HI! I keep on getting this error and I have tried different things but I am
not sure how to send the expiring date.
The error that I am getting in Firefox 1.5 is
"Error: expires.toGMTString is not a function"
----------------------------------------------------
I have this in a .js file and in the head section.
|
by: Ivan Liu |
last post by:
Hi,
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
Ivan
|
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...
| |
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: 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: 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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |