0

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...

Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
  • 1
    You don't have an object here, I really don't understand what you want. Do you want to have a method saved to an object so that when you call the method it does something with/to that object? – General Grey May 31 '12 at 12:06
  • Yes, I want to have a method saved to an object so that way I can call the object to invoke the method. Sorry for my terrible english... – Lost_In_Library May 31 '12 at 12:09
  • 1
    I think what SLaks answered below is what you want. But if you are looking more for what I described that is pretty easy too, but I think SLaks is right, try delegates. – General Grey May 31 '12 at 12:16

3 Answers3

2

You're trying to use delegates:

operation = TakeDataFromGui;
...
operation();
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

use a lambda expression to assign the delegate:

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

invoke the action like a regular function:

operation()
jeroenh
  • 26,362
  • 10
  • 73
  • 104
1

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();
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38