472,978 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 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 1330
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.