locked
Use this and base together? RRS feed

  • Question

  • Hi,
    I was wondering if there is any way that I can use this() and base() together in a constructor. I mean a construct like this:

    public MyClass(string arg1, string arg2): base(arg1), this(arg2)
    {
    }

    thanks
    Thomas
    Monday, December 4, 2006 9:00 AM

Answers

  • No, you cannot do it, because "this(arg2)" constructor will also call base constructor.

    General recommendation is to create the most general constructor with all possible arguments that will be used by other more specific constructors. For your example it may look like:

    public MyClass(string arg1, string arg2): base(arg1)
    {
        _arg2 = arg2;
        //Other initializations
    }

    public MyClass(string arg2): this(null, arg2)
    {
    }

    public MyClass(): this(null, null)
    {
    }

    Monday, December 4, 2006 9:13 AM
  •  littleguru wrote:

    This is not always suitable if you don't want to allow null being passed in!

    Than you shouldn't create   constructors that initialize the object incorrectly, like following one. If arg1 cannot be null, it must be specified through constructor parameter. It it has default value (in my example it is null), than you may create a constructor without specifying arg1.

    public MyClass(string arg2): this(null, arg2)
    {
    }

    Monday, December 4, 2006 9:56 AM

All replies

  • No, you cannot do it, because "this(arg2)" constructor will also call base constructor.

    General recommendation is to create the most general constructor with all possible arguments that will be used by other more specific constructors. For your example it may look like:

    public MyClass(string arg1, string arg2): base(arg1)
    {
        _arg2 = arg2;
        //Other initializations
    }

    public MyClass(string arg2): this(null, arg2)
    {
    }

    public MyClass(): this(null, null)
    {
    }

    Monday, December 4, 2006 9:13 AM
  • Hi,

    no you can not. If you have code that should be executed by any of your constructors but have to invoke the base constructor you need to create a method that is invoked by all constructors. For example:

    public MyClass(string arg1, string arg2) : base(arg1)
    {
        Initialize(arg2);
    }

    public MyClass(string arg2)
    {
        Initialize(arg2);
    }

    private void Initialize(string arg)
    {
        // TODO: Implement your code here.
    }

    Monday, December 4, 2006 9:14 AM
  •  Sergei Almazov wrote:

    No, you cannot do it, because "this(arg2)" constructor will also call base constructor.

    General recommendation is to create the most general constructor with all possible arguments that will be used by other more specific constructors. For your example it may look like:

    public MyClass(string arg1, string arg2): base(arg1)
    {
        _arg2 = arg2;
        //Other initializations
    }

    public MyClass(string arg2): this(null, arg2)
    {
    }

    public MyClass(): this(null, null)
    {
    }

    This is not always suitable if you don't want to allow null being passed in!

    Monday, December 4, 2006 9:15 AM
  •  littleguru wrote:

    This is not always suitable if you don't want to allow null being passed in!

    Than you shouldn't create   constructors that initialize the object incorrectly, like following one. If arg1 cannot be null, it must be specified through constructor parameter. It it has default value (in my example it is null), than you may create a constructor without specifying arg1.

    public MyClass(string arg2): this(null, arg2)
    {
    }

    Monday, December 4, 2006 9:56 AM
  •  Sergei Almazov wrote:
     littleguru wrote:

    This is not always suitable if you don't want to allow null being passed in!

    Than you shouldn't create   constructors that initialize the object incorrectly, like following one. If arg1 cannot be null, it must be specified through constructor parameter. It it has default value (in my example it is null), than you may create a constructor without specifying arg1.

    public MyClass(string arg2): this(null, arg2)
    {
    }

    It's still the same. It's not suitable in all cases. Creating a method that is invoked on object creation is sometimes better, as it provides a better way to customize the object creation process. Other then creating constructors and pass in null values!

    Monday, December 4, 2006 1:42 PM
  • By looking more carefully I indeed found that by re-organizing my constructors (putting the logic in the most detailed ctor, the one with the most parameters), the combination of this and base is never necessary...
    Monday, December 4, 2006 4:27 PM