none
RelayCommand constructors RRS feed

  • Question

  • public RelayCommand(Action<T> execute) : this(execute, null) { }
    i want to understand what exactly that means and how it works, specially the colon and "this"?
    Saturday, December 14, 2019 5:48 AM

Answers

  • Hi,
    there's no inheritance of constructors. First and second constructor are overloaded methods (with differently signatures). The first method calls the second method. Without calling the second method you can write:

      public class RelayCommand : ICommand
      {
        #region Fields
    
        private readonly Predicate<object> _canExecute = null;
        private readonly Action<object> _execute;
    
        #endregion
    
        #region Constructors
    
        public RelayCommand(Action<object> execute) 
        {
          if (execute == null) throw new ArgumentNullException("execute");
          _execute = execute;
        }
    
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
          if (execute == null) throw new ArgumentNullException("execute");
          _execute = execute; _canExecute = canExecute;
        }
    ...


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Saturday, December 14, 2019 4:19 PM
    • Marked as answer by essamce Saturday, December 14, 2019 4:41 PM
    Saturday, December 14, 2019 4:19 PM

All replies

  • Hi,
    this constructor (with only one parameter) call the other constructor in the same class (with two parameters and null for the second parameter). The class can be used with typed parameters("T") or with fixed type like "object" like in the following demo:

      public class RelayCommand : ICommand
      {
        #region Fields
    
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;
    
        #endregion
    
        #region Constructors
    
    // this constructor call the other constructor
        public RelayCommand(Action<object> execute) : this(execute, null) { }
    
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
          if (execute == null) throw new ArgumentNullException("execute");
          _execute = execute; _canExecute = canExecute;
        }
    ...


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, December 14, 2019 11:18 AM
  • so the first constructor inherented from the second one or inherited from the RelayCommand?

    if so can you rewrite the first constructor without inheritance so i can understand the idea 

    Saturday, December 14, 2019 3:00 PM
  • Hi,
    there's no inheritance of constructors. First and second constructor are overloaded methods (with differently signatures). The first method calls the second method. Without calling the second method you can write:

      public class RelayCommand : ICommand
      {
        #region Fields
    
        private readonly Predicate<object> _canExecute = null;
        private readonly Action<object> _execute;
    
        #endregion
    
        #region Constructors
    
        public RelayCommand(Action<object> execute) 
        {
          if (execute == null) throw new ArgumentNullException("execute");
          _execute = execute;
        }
    
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
          if (execute == null) throw new ArgumentNullException("execute");
          _execute = execute; _canExecute = canExecute;
        }
    ...


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Saturday, December 14, 2019 4:19 PM
    • Marked as answer by essamce Saturday, December 14, 2019 4:41 PM
    Saturday, December 14, 2019 4:19 PM