locked
RelayCommand constructors RRS feed

  • Вопрос

  • 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"?
    14 декабря 2019 г. 5:48

Ответы

  • 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


    • Изменено Peter Fleischer 14 декабря 2019 г. 16:19
    • Помечено в качестве ответа essamce 14 декабря 2019 г. 16:41
    14 декабря 2019 г. 16:19

Все ответы

  • 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

    14 декабря 2019 г. 11:18
  • 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 

    14 декабря 2019 г. 15:00
  • 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


    • Изменено Peter Fleischer 14 декабря 2019 г. 16:19
    • Помечено в качестве ответа essamce 14 декабря 2019 г. 16:41
    14 декабря 2019 г. 16:19