Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How does the method work ?

How does the method work ?

Scheduled Pinned Locked Moved C#
question
10 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hello there, Would you please explain , how the below method (that is bold) works ?

    public class RelayCommand : ICommand
    {
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    **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;           
    }
    
    \[DebuggerStepThrough\]
    public bool CanExecute(object parameter)
    {
        return \_canExecute == null ? true : \_canExecute(parameter);
    }
    
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    
    public void Execute(object parameter)
    {
        \_execute(parameter);
    }
    

    }

    I mean , what's the meaning of : this(execute, null) ?

    S P 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hello there, Would you please explain , how the below method (that is bold) works ?

      public class RelayCommand : ICommand
      {
      readonly Action<object> _execute;
      readonly Predicate<object> _canExecute;

      **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;           
      }
      
      \[DebuggerStepThrough\]
      public bool CanExecute(object parameter)
      {
          return \_canExecute == null ? true : \_canExecute(parameter);
      }
      
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
      
      public void Execute(object parameter)
      {
          \_execute(parameter);
      }
      

      }

      I mean , what's the meaning of : this(execute, null) ?

      S Offline
      S Offline
      Steve Westbrook
      wrote on last edited by
      #2

      It calls the constructor below it: it's like base(whatever). The point is to avoid duplicating whatever functionality is in the two-parameter method.

      M 2 Replies Last reply
      0
      • S Steve Westbrook

        It calls the constructor below it: it's like base(whatever). The point is to avoid duplicating whatever functionality is in the two-parameter method.

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Thanks, What's the name of these methods ? Is there a specific name for these methods ?

        L 1 Reply Last reply
        0
        • M Mohammad Dayyan

          Thanks, What's the name of these methods ? Is there a specific name for these methods ?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          It works on constructors only, i.e. methods that: - have no return type; - have a name identical to the class name; - get invoked using the new keyword. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          modified on Friday, February 5, 2010 4:29 PM

          M 1 Reply Last reply
          0
          • L Luc Pattyn

            It works on constructors only, i.e. methods that: - have no return type; - have a name identical to the class name; - get invoked using the new keyword. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            [The QA section does it automatically now, I hope we soon get it on regular forums as well]


            modified on Friday, February 5, 2010 4:29 PM

            M Offline
            M Offline
            Mohammad Dayyan
            wrote on last edited by
            #5

            Got it ;)

            1 Reply Last reply
            0
            • S Steve Westbrook

              It calls the constructor below it: it's like base(whatever). The point is to avoid duplicating whatever functionality is in the two-parameter method.

              M Offline
              M Offline
              Mohammad Dayyan
              wrote on last edited by
              #6

              So, this(execute, null) means something like base(execute, null) :confused:

              L 1 Reply Last reply
              0
              • M Mohammad Dayyan

                So, this(execute, null) means something like base(execute, null) :confused:

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                No, they are different. use base to refer to the base class, the class from which the current class derives. this refers to some other constructor of the current class; the normal use is: - one implements the "full" constructor, the one taking most parameters; - then one refers all other constructors to the first one, by providing default parameters for the missing ones. See your own example. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                modified on Friday, February 5, 2010 4:28 PM

                M 1 Reply Last reply
                0
                • M Mohammad Dayyan

                  Hello there, Would you please explain , how the below method (that is bold) works ?

                  public class RelayCommand : ICommand
                  {
                  readonly Action<object> _execute;
                  readonly Predicate<object> _canExecute;

                  **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;           
                  }
                  
                  \[DebuggerStepThrough\]
                  public bool CanExecute(object parameter)
                  {
                      return \_canExecute == null ? true : \_canExecute(parameter);
                  }
                  
                  public event EventHandler CanExecuteChanged
                  {
                      add { CommandManager.RequerySuggested += value; }
                      remove { CommandManager.RequerySuggested -= value; }
                  }
                  
                  public void Execute(object parameter)
                  {
                      \_execute(parameter);
                  }
                  

                  }

                  I mean , what's the meaning of : this(execute, null) ?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  this(execute, null) passes the call through to the second constructor - setting the predicate to null. As you can't call a constructor by name, you need to do this to actually call the second constructor. As you know, in a method, you can do the following:

                  public void DoThis(int index)
                  {
                  DoThis(index, null);
                  }

                  public void DoThis(int index, string message)
                  {
                  }

                  While it would be nice to do this with a constructor, you can't:

                  public MyClass()
                  {
                  MyClass(null);
                  }

                  public MyClass(string message)
                  {
                  }

                  In order to get the same effect, you modify the constructor definition instead.

                  public MyClass() : this(null)
                  {
                  }

                  public MyClass(string message)
                  {
                  }

                  Now, this differs from calling base which invokes a method on the parent class, rather than this class. What you can't do, is combine this and base in a constructor - it's one or the other.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  M 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    this(execute, null) passes the call through to the second constructor - setting the predicate to null. As you can't call a constructor by name, you need to do this to actually call the second constructor. As you know, in a method, you can do the following:

                    public void DoThis(int index)
                    {
                    DoThis(index, null);
                    }

                    public void DoThis(int index, string message)
                    {
                    }

                    While it would be nice to do this with a constructor, you can't:

                    public MyClass()
                    {
                    MyClass(null);
                    }

                    public MyClass(string message)
                    {
                    }

                    In order to get the same effect, you modify the constructor definition instead.

                    public MyClass() : this(null)
                    {
                    }

                    public MyClass(string message)
                    {
                    }

                    Now, this differs from calling base which invokes a method on the parent class, rather than this class. What you can't do, is combine this and base in a constructor - it's one or the other.

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    M Offline
                    M Offline
                    Mohammad Dayyan
                    wrote on last edited by
                    #9

                    excellent, I've got it. :rose:

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      No, they are different. use base to refer to the base class, the class from which the current class derives. this refers to some other constructor of the current class; the normal use is: - one implements the "full" constructor, the one taking most parameters; - then one refers all other constructors to the first one, by providing default parameters for the missing ones. See your own example. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                      modified on Friday, February 5, 2010 4:28 PM

                      M Offline
                      M Offline
                      Mohammad Dayyan
                      wrote on last edited by
                      #10

                      very good, Thanks dude ;)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups