Solved

pass function names?

Posted on 2015-01-17
4
95 Views
Last Modified: 2015-01-20
void QuestionControlPayForDiapers_nextclicked(object sender, EventArgs e)
        {
            ShowNameCityPayDiapersPage(false);

            if (QuestionControlPayForDiapers.DropDownText == "Yes")
            {
                ShowDiaperSizesPage(true);
            }
            else
            {
                ShowChuxWipesPage(true);
            }           
        }

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)
        {
            hidegroup(false);

            if (dropdownquestioncontrol.DropDownText == "Yes")
            {
                showyesgroup(true);
            }
            else
            {
                shownogroup(true);
            }           
        }

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!
0
Comment
Question by:knowlton
4 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 50 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.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 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.
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 50 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.
0
 
LVL 5

Author Closing Comment

by: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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now