Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


pass function names?

Posted on 2015-01-17
Medium Priority
Last Modified: 2015-01-20
void QuestionControlPayForDiapers_nextclicked(object sender, EventArgs e)

            if (QuestionControlPayForDiapers.DropDownText == "Yes")

Open in new window

I want to turn the method above into a more generic version that I can use over and over, passing in names of methods to call and/or names of controls:

void QuestionControlPayForDiapers_nextclicked(object sender, EventArgs e)
             ProcessNextButtonClick(ShowNameCityPayDiapers, QuestionControlPayForDiapers, ShowDiaperSizesPage, ShowChuxWipesPage);

void ProcessNextButtonClick(function hidegroup, QuestionControl dropdownquestioncontrol, function showyesgroup, function shownogroup)

            if (dropdownquestioncontrol.DropDownText == "Yes")

Open in new window

Update:  So far my own research seems to indicate that using "delegates" may provide the answer.

In any case -- I could still use some examples .. some working code that does what I have asked.  Thanks!
Question by:Tom Knowlton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 200 total points
ID: 40555647
Delegates could be one way to go as shown here, but it forces to create one delegate for every method signature.
if you are using .NET 3.5+ and your methods return a value, there is a nice alternative that uses Func type and allows  a more generic solution.
LVL 75

Accepted Solution

käµfm³d   👽 earned 1600 total points
ID: 40555804
...but it forces to create one delegate for every method signature.
I mean, you could, but you don't have to. You can use the built-in Action and Func delegates as appropriate. Using delegates is a great way to reuse the same algorithm, but change it's internal behavior. That seems to be more or less what you are talking about here, though I would caution you to not do this just for the sake of having shorter code. Using delegates should have a programmatic goal. The use of delegates, if done incorrectly, can make your code harder to follow.
LVL 11

Assisted Solution

louisfr earned 200 total points
ID: 40555831
To use delegates is trivial. Just replace the "function" in your sample with "Func<bool>" and it should work.
void ProcessNextButtonClick(Func<bool> hidegroup, QuestionControl dropdownquestioncontrol, Func<bool> showyesgroup, Func<bool> shownogroup)

Open in new window

The rest of your sample looks ok.

Author Closing Comment

by:Tom Knowlton
ID: 40560581
Thanks everyone!

Yeah - it was delegates I was after.

I'd used them before, but only in a strictly "Observer" type scenario where a control generates and event that a parent (webform) consumes.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question