3 Replies - 144 Views - Last Post: 11 November 2019 - 08:57 PM Rate Topic: -----

#1 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 22-February 17

Understanding this use of the Action delegate

Posted 10 November 2019 - 08:02 PM

Hi

I can understand the basic definition of the Action delegate as per the following example:

Action <int> printActionDel = new Action<int>(ConsolePrint);



Here a delegate which takes one integer input (void return) is defined, a new instance is created and points to the ConsolePrint method.

However, the following stumps me (saw it in a tut on the ICommand Interface I am working on):

public Action<object> ExecuteMethod { get; set; }



There is no assigment (=) symbol so is ExecuteMethod a delegate of type Action?

Also{get; set;} are usually associated with properties so what is the meaning of having them here?

I'd be most grateful if someone could explain please.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding this use of the Action delegate

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15356
  • View blog
  • Posts: 61,554
  • Joined: 12-June 08

Re: Understanding this use of the Action delegate

Posted 11 November 2019 - 10:40 AM

I would have to see the larger tutorial to comment on what's up, but it works like properties and winform events. You can assign them in a larger scope or inside a method.
Was This Post Helpful? 0
  • +
  • -

#3 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 22-February 17

Re: Understanding this use of the Action delegate

Posted 11 November 2019 - 01:14 PM

Ok, here is the class it is from:

class RelayCommand : ICommand
    {
        public Action<object> ExecuteMethod { get; set; }
        
        public Func<object, bool> CanExecuteMethod{get; set;}
        
        public RelayCommand (Action<object> executeMethod, Func<object, bool> canExecuteMethod)
        {
            this.CanExecuteMethod = canExecuteMethod;
            this.ExecuteMethod = executeMethod;
        }
        

        public bool CanExecute(object parameter)
        {
            return CanExecuteMethod(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            ExecuteMethod(parameter);
        }
    }



And here is the SampleViewModel class:

class SampleViewModel
    {
        public RelayCommand relayCommand { get; set; }

        public SampleViewModel()
        {
            relayCommand = new RelayCommand(Execute,CanExecute);
        }

        public bool CanExecute(object param)
        {
            return true;
        }

        public void Execute(object param)
        {
            // Writecode here for when button is clicked
            MessageBox.Show("Button Clicked");
        }
    }



Is this sufficient?

The objective is to use the ICommand to implement some code when a button is clicked on a Winform.

Thank you kindly. It is really much appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,221
  • Joined: 05-May 12

Re: Understanding this use of the Action delegate

Posted 11 November 2019 - 08:57 PM

ExecuteMethod is being declared as a property, hence the getter and setter.

That property is being declared to hold an Action<object> delegate.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1