Question

as you can see in my code, I want to hold( set ) a method into an object. And call the method with that object...

Please take look at the code, you can understand easly; it's hard to explain in plain English...

My question is; Ofcourse "Action operation" object can't hold the method that will be called.

So, how can I solve this problem? What can I do?


...
    enum CampaignUserChoice
    {
        Insert,
        Update,
        Delete,
        Disable
    }

private void AskUserAboutCampaignOperation(CampaignUserChoice choice) { string questionForUser = string.Empty; string questionTitleForUser = string.Empty; Action operation; //<<<<--------------------- this line, it's method holder if (choice == CampaignUserChoice.Insert) { questionForUser = "Do you want to create a new campaign?"; questionTitleForUser = "NEW CAMPAIGN"; operation = InsertCampaign(TakeDatasFromGui()); //<---------- set which method you want to call } else { operation = UpdateCampaign( campaignId, TakeDatasFromGui()); } //TODO write other elses... switch (MessageBox.Show(questionForUser, questionTitleForUser, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)) { case DialogResult.Yes: operation; //<<<------------------ call method here... break; case DialogResult.No: // "No" processing break; case DialogResult.Cancel: // "Cancel" processing break; } }

Thank you very much for all answers...

Was it helpful?

Solution

You're trying to use delegates:

operation = TakeDataFromGui;
...
operation();

OTHER TIPS

use a lambda expression to assign the delegate:

operation = () => InsertCampaign(TakeDatasFromGui()); 

invoke the action like a regular function:

operation()

You need delegate to call method

  //declare delegate declaration same as function
   delegate returntype delegate_name(parameter1,paramenter2,..);

   //assaign function to  delegate
   delegate_name=function();

   //call function
   delagate_name();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top