473,804 Members | 3,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to make generalized methods that accept parameterized references to call other methods?

I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing .MakeQuickResiz e();
ImageProcessing .Sharpen();
FileOps.CreateY earAndMonthAndD ayDirectoryBase dOnDate()
Sender.EmailCom maDelimitedList ()

What would be really cool is if I could make a generalized function that
would accept other methods as parameters, and would perform the methods on
another parameterized item that's fed to the function. So we might have the
equivalent of the following

public void ExecuteParamete rizedMethodsOnE veryFileInADire ctory( string
targetPath, MakeQuickResize (100,100), Sharpen(), ConvertToGray() ,
RegisterInDb(), RenameByDBRefer ence() ) ...

Is what I'm speculating about possible? Is this something that maybe
delegates are good for? (Have never used them...) If it is, can someone
suggest the right syntax? What I posit above is clearly wrong syntactically.

Finally, if it is possible to chain up methods in this way, I'm wondering if
there's any way to pass around return types to other members of the chain. A
hypothetical example would be:
Method 1 registers a filename in a database system, and a new filename is
derived from the DateTime stamp of creation
Method 2 uses that filename that was returned as a string to execute
something else.

Thanks for any insight you can offer.

-KF
Jun 22 '07 #1
3 1369
Yes, you can use delegates to pass functions as parameters.

A basis for what you need could be something like this

public delegate int intproc1(string fn);
public delegate int intproc2(string fn1, string fn2);

int GetLength(strin g s)
{
return s.Length;
}

int GetTwiceLength( string s)
{
return s.Length * 2;
}

int GetDoubleLength (string s1, string s2)
{
return s1.Length + s2.Length;
}

public void Ex(string s, intproc2 p2, params intproc1[] procs)
{
Console.WriteLi ne ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLi ne ( ip(s).ToString () );
}
}

public void DoTest()
{
intproc1 p1 = GetLength;
intproc1 p2 = GetTwiceLength;

Ex("This string", GetDoubleLength , GetLength, GetTwiceLength, p2, p1);
Ex("Another string", GetDoubleLength );
}
<ke*****@nospam .nospamwrote in message
news:ew******** ******@TK2MSFTN GP03.phx.gbl...
>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing .MakeQuickResiz e();
ImageProcessing .Sharpen();
FileOps.CreateY earAndMonthAndD ayDirectoryBase dOnDate()
Sender.EmailCom maDelimitedList ()

What would be really cool is if I could make a generalized function that
would accept other methods as parameters, and would perform the methods on
another parameterized item that's fed to the function. So we might have
the equivalent of the following

public void ExecuteParamete rizedMethodsOnE veryFileInADire ctory( string
targetPath, MakeQuickResize (100,100), Sharpen(), ConvertToGray() ,
RegisterInDb(), RenameByDBRefer ence() ) ...

Is what I'm speculating about possible? Is this something that maybe
delegates are good for? (Have never used them...) If it is, can someone
suggest the right syntax? What I posit above is clearly wrong
syntactically.

Finally, if it is possible to chain up methods in this way, I'm wondering
if there's any way to pass around return types to other members of the
chain. A hypothetical example would be:
Method 1 registers a filename in a database system, and a new filename is
derived from the DateTime stamp of creation
Method 2 uses that filename that was returned as a string to execute
something else.

Thanks for any insight you can offer.

-KF
Jun 22 '07 #2

Exactly what I needed to know, Ian. Thanks so much for your clear and
concise code, it all makes sense. This will be very helpful.

Just curious: does anyone know when delegates were first invented and
generally implemented as a language construct? I find the evolution of
programming languages very interesting: it's fascinating how abstract and
high-level languages like C# have become, and I'm interested in the history
of how we got here.

-KF

"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Yes, you can use delegates to pass functions as parameters.

A basis for what you need could be something like this

public delegate int intproc1(string fn);
public delegate int intproc2(string fn1, string fn2);

int GetLength(strin g s)
{
return s.Length;
}

int GetTwiceLength( string s)
{
return s.Length * 2;
}

int GetDoubleLength (string s1, string s2)
{
return s1.Length + s2.Length;
}

public void Ex(string s, intproc2 p2, params intproc1[] procs)
{
Console.WriteLi ne ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLi ne ( ip(s).ToString () );
}
}

public void DoTest()
{
intproc1 p1 = GetLength;
intproc1 p2 = GetTwiceLength;

Ex("This string", GetDoubleLength , GetLength, GetTwiceLength, p2, p1);
Ex("Another string", GetDoubleLength );
}
<ke*****@nospam .nospamwrote in message
news:ew******** ******@TK2MSFTN GP03.phx.gbl...
>>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessin g.MakeQuickResi ze();
ImageProcessin g.Sharpen();
FileOps.Create YearAndMonthAnd DayDirectoryBas edOnDate()
Sender.EmailCo mmaDelimitedLis t()

What would be really cool is if I could make a generalized function that
would accept other methods as parameters, and would perform the methods
on another parameterized item that's fed to the function. So we might
have the equivalent of the following

public void ExecuteParamete rizedMethodsOnE veryFileInADire ctory( string
targetPath, MakeQuickResize (100,100), Sharpen(), ConvertToGray() ,
RegisterInDb() , RenameByDBRefer ence() ) ...

Is what I'm speculating about possible? Is this something that maybe
delegates are good for? (Have never used them...) If it is, can someone
suggest the right syntax? What I posit above is clearly wrong
syntacticall y.

Finally, if it is possible to chain up methods in this way, I'm wondering
if there's any way to pass around return types to other members of the
chain. A hypothetical example would be:
Method 1 registers a filename in a database system, and a new filename is
derived from the DateTime stamp of creation
Method 2 uses that filename that was returned as a string to execute
something else.

Thanks for any insight you can offer.

-KF

Jun 22 '07 #3
The first widely used language to pass functions as parameters was Lisp,
invented in 1958 by John McCarthy. There is a nice history on Wikipedia.

<ke*****@nospam .nospamwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>
Exactly what I needed to know, Ian. Thanks so much for your clear and
concise code, it all makes sense. This will be very helpful.

Just curious: does anyone know when delegates were first invented and
generally implemented as a language construct? I find the evolution of
programming languages very interesting: it's fascinating how abstract and
high-level languages like C# have become, and I'm interested in the
history of how we got here.

-KF

"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Yes, you can use delegates to pass functions as parameters.

A basis for what you need could be something like this

public delegate int intproc1(string fn);
public delegate int intproc2(string fn1, string fn2);

int GetLength(strin g s)
{
return s.Length;
}

int GetTwiceLength( string s)
{
return s.Length * 2;
}

int GetDoubleLength (string s1, string s2)
{
return s1.Length + s2.Length;
}

public void Ex(string s, intproc2 p2, params intproc1[] procs)
{
Console.WriteLi ne ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLi ne ( ip(s).ToString () );
}
}

public void DoTest()
{
intproc1 p1 = GetLength;
intproc1 p2 = GetTwiceLength;

Ex("This string", GetDoubleLength , GetLength, GetTwiceLength, p2, p1);
Ex("Another string", GetDoubleLength );
}
<ke*****@nospa m.nospamwrote in message
news:ew******* *******@TK2MSFT NGP03.phx.gbl.. .
>>>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessi ng.MakeQuickRes ize();
ImageProcessi ng.Sharpen();
FileOps.Creat eYearAndMonthAn dDayDirectoryBa sedOnDate()
Sender.EmailC ommaDelimitedLi st()

What would be really cool is if I could make a generalized function that
would accept other methods as parameters, and would perform the methods
on another parameterized item that's fed to the function. So we might
have the equivalent of the following

public void ExecuteParamete rizedMethodsOnE veryFileInADire ctory( string
targetPath, MakeQuickResize (100,100), Sharpen(), ConvertToGray() ,
RegisterInDb( ), RenameByDBRefer ence() ) ...

Is what I'm speculating about possible? Is this something that maybe
delegates are good for? (Have never used them...) If it is, can someone
suggest the right syntax? What I posit above is clearly wrong
syntactically .

Finally, if it is possible to chain up methods in this way, I'm
wondering if there's any way to pass around return types to other
members of the chain. A hypothetical example would be:
Method 1 registers a filename in a database system, and a new filename
is derived from the DateTime stamp of creation
Method 2 uses that filename that was returned as a string to execute
something else.

Thanks for any insight you can offer.

-KF


Jun 22 '07 #4

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

Similar topics

0
3033
by: totierne | last post by:
comp.databases.ms-access, I want to know how to use Oracle views with session variables in Access. The parameterised views in access, are migrated to views with per session variables. The open questions: How to display a resultset
3
1531
by: Chris Calzaretta | last post by:
From: "Chris Calzaretta" <ccalzaretta@hotmail.com> Subject: Re: Is It Possible? Date: Friday, February 04, 2005 11:44 AM Ok i am posting the code I need to create a form from this web service http://24.163.239.122/wsprojecttrackerobjects/wsprojecttrackerobjects.asmx if you call getloginscreen there is a field called screendecription that field screendecription has the form code I am trying to create at runtime
6
1424
by: PJ6 | last post by:
I would like to refer to properties in code without having to resort to using a string for the name. AddessOf gives me this ability for methods, but I can't find a single way to point that at the Get or Set methods of properties directly. Why don't I want to use a string? Well, I want to know at design time that my property references aren't broken, much the same way I can have this when using regular delegates. Is this possible?
6
5947
by: Ian Boyd | last post by:
Every time during development we had to make table changes, we use Control Center. Most of the time, Control Center fails. If you try to "undo all", it doesn't, and you end up losing your identity seed, or your constraints, or your triggers, or your table. Talking to developers at other companies who have had the misfortune of using DB2, they are adamant that you cannot use the tools; they are buggy and you just have to resign yourself to...
3
2772
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
0
1362
by: Peter Morris [Droopy eyes software] | last post by:
I have solved this problem but it involved a static class, reflection, and a lot of code. I wanted to post it here because the solution using virtual class methods takes only a few lines of code. I have an interface ISignalTarget AcceptSignalPermission MayAcceptSignal(Signal signal); void AcceptSignal(Signal signal); (Accept signal permission is just an enum)
3
10926
by: Dmitry | last post by:
I am trying to figure out how to pass set of credentials to System.IO Challenge is: App is running under one set of credentials, but via GUI user have a chance to enter another set. I would like to be able to use supplied credentials with System.IO versus using default credentials that app is running under. So far I am forced to use WMI which is less convenient and slower then System.IO, but it's providing me with "Connection Options"
1
2428
by: gregory.lielens | last post by:
Hello, We are currently writing python bindings to an existing C++ library, and we encountered a problem that some of you may have solved (or that has found unsolvable :( ): A C++ class (let's call it CClass) is binded using classical Python extension API to _PClass, which is accesible through python without any
49
5815
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current name search scope. i.e. a very simple application would be class ManyComputations { calling System.Math;
0
9710
marktang
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...
0
10593
Oralloy
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...
1
10329
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,...
1
7626
isladogs
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...
0
6858
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
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.