471,337 Members | 907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 1236
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Chris Calzaretta | last post: by
6 posts views Thread by Ian Boyd | last post: by
reply views Thread by Peter Morris [Droopy eyes software] | last post: by
49 posts views Thread by Ben Voigt [C++ MVP] | last post: by
reply views Thread by rosydwin | last post: by

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.