Vertika Misra
Vertika Misra

Reputation: 29

Constructor overriding another constructor in same class: Error

I have two constructors, in same class, with multiple arguments (c# on visual studio):

public class Example
{
 {
 public string arg1 { get; set; }
 public string arg2 { get; set; }
 public string arg3 { get; set; }
 public string arg4 { get; set; }
 public string arg5 { get; set; }
 public string arg6 { get; set; }
 }

public Example(string arg1, string arg2, string arg3){
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
}

public Example(string arg4, string arg5, string arg6){
this.arg4 = arg4;
this.arg5 = arg5;
this.arg6 = arg6;
 }
}

In a separate aspx.cs file, under protected string method, I'm calling these two constructor based on condition.

Example ExampleObj = null;

protected string Method(object sender, EventArgs e){

 if (condition){
  ExampleObj = Constructor2(parameter1, parameter2, parameter3, parameter4, parameter5, parameter6);
 }
 else
 {
  ExampleObj = Constructor1(parameter1, parameter2, parameter3);
 }

}

If condition is true, I want to call second constructor, with the arguments in addition to arguments in Constructor1. I think, I can say constructor2 overrides or extends the constructor1. After all my research, I tried

public Constructor2(string arg4, string arg5, string arg6)
:this(arg1, arg2, arg3) {
 this.arg4 = arg4;
 this.arg5 = arg5;
 this.arg6 = arg6;
}

Still, I'm getting error 'an object reference is required' for arguments in this(). As per my requirements, I can't make my main method static. I found some solutions saying create instances or change to static, but I'm unable to apply that on my code (getting errors).

Also, I'm not getting it clear if I should state datatype in this() constructor. Please, help me if anyone knows the solution to this. I might be missing something.

Thanks!

Upvotes: 1

Views: 918

Answers (2)

Zohar Peled
Zohar Peled

Reputation: 82524

The c# compiler can't distinguish between the two constructors since they have the same signature (method name, number and types of parameters). If you had a different number of arguments for each constructor, or if any of your arguments was of a different type, your code would compile.

I'm guessing you are looking for something like this:

public class Example
{
    public string arg1 { get; set; }
    public string arg2 { get; set; }
    public string arg3 { get; set; }
    public string arg4 { get; set; }
    public string arg5 { get; set; }
    public string arg6 { get; set; }

    public Example(string arg1, string arg2, string arg3)
    {
        this.arg1 = arg1;
        this.arg2 = arg2;
        this.arg3 = arg3;
    }

    public Example(string arg1, string arg2, string arg3, string arg4, string arg5, string arg6)
         : this(arg1, arg2, arg3)
    {
        this.arg4 = arg4;
        this.arg5 = arg5;
        this.arg6 = arg6;
    }
}

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273784

A valid constructor should have the same name as the class.

You can have 2 constructors but they need to have the same name and different parameter lists. Otherwise the compiler can't tell them apart when you use them.

This works Ok:

  class Example
  {
    public Example(string arg1)
    { 
      this.arg1 = arg1;
    }

    public Example(string arg1, string arg2)
        :this(arg1) 
    {
      this.arg2 = arg2;
    }
  }

Upvotes: 2

Related Questions