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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate()
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
 
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);
}
 
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 said:
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);
}


I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate()
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
 
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.

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 said:
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);
}


I am enjoying making generalized methods to serve common needs, such as:
ImageProcessing.MakeQuickResize();
ImageProcessing.Sharpen();
FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate()
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top