Use this and base together?  
Author Message
guppie





PostPosted: Visual C# General, Use this and base together? Top

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


Visual C#13  
 
 
Sergei Almazov





PostPosted: Visual C# General, Use this and base together? Top

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)
{
}


 
 
littleguru





PostPosted: Visual C# General, Use this and base together? Top

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.
}



 
 
littleguru





PostPosted: Visual C# General, Use this and base together? Top

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!



 
 
Sergei Almazov





PostPosted: Visual C# General, Use this and base together? Top

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)
{
}


 
 
littleguru





PostPosted: Visual C# General, Use this and base together? Top

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!



 
 
guppie





PostPosted: Visual C# General, Use this and base together? Top

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...