473,394 Members | 1,699 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,394 software developers and data experts.

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.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDa te()
Sender.EmailCommaDelimitedList()

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 ExecuteParameterizedMethodsOnEveryFileInADirectory ( string
targetPath, MakeQuickResize(100,100), Sharpen(), ConvertToGray(),
RegisterInDb(), RenameByDBReference() ) ...

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 1348
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(string 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.WriteLine ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLine ( 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**************@TK2MSFTNGP03.phx.gbl...
>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDa te()
Sender.EmailCommaDelimitedList()

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 ExecuteParameterizedMethodsOnEveryFileInADirectory ( string
targetPath, MakeQuickResize(100,100), Sharpen(), ConvertToGray(),
RegisterInDb(), RenameByDBReference() ) ...

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****@rocketcomp.com.auwrote in message
news:%2****************@TK2MSFTNGP03.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(string 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.WriteLine ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLine ( 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**************@TK2MSFTNGP03.phx.gbl...
>>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnD ate()
Sender.EmailCommaDelimitedList()

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 ExecuteParameterizedMethodsOnEveryFileInADirectory ( string
targetPath, MakeQuickResize(100,100), Sharpen(), ConvertToGray(),
RegisterInDb(), RenameByDBReference() ) ...

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 #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****************@TK2MSFTNGP05.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****@rocketcomp.com.auwrote in message
news:%2****************@TK2MSFTNGP03.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(string 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.WriteLine ( p2 ( s, s ).ToString () );

foreach (intproc1 ip in procs)
{
Console.WriteLine ( 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**************@TK2MSFTNGP03.phx.gbl...
>>>I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOn Date()
Sender.EmailCommaDelimitedList()

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 ExecuteParameterizedMethodsOnEveryFileInADirectory ( string
targetPath, MakeQuickResize(100,100), Sharpen(), ConvertToGray(),
RegisterInDb(), RenameByDBReference() ) ...

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
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...
3
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...
6
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...
6
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...
3
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...
0
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....
3
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...
1
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...
49
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...

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.