3

I have a partial class with a constructor, but the constructor is throwing an error because 'a member with the same signature is already declared' (a constructor of the same name exists in the other partial class). How do I make a constructor for a partial class when the name is already being used?

public partial class DigitalArchivesAssetsDataContext
{
    public DigitalArchivesAssetsDataContext()
        : base(System.Configuration.ConfigurationManager.ConnectionStrings["digitalArchivesAssets"].ConnectionString, mappingSource)
    {
        OnCreated();
    }
}
Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130
  • 2
    If the constructor signature is the same, why do you need it in two different partial classes? Ultimately the partial classes will be compiled as if they were written in the same class. – keyboardP Jul 12 '16 at 19:59
  • You cant have duplicate members in your class. Partial is nothing more than allowing the compiler to compile the class from multiple files but the same coding principles apply including not having duplicate members (constructors included). – Igor Jul 12 '16 at 20:04
  • Is the method you are showing in your code the existing method or what you are trying to add? If its what is already there then the purpose of it is to make use of `OnCreated` to add your custom code that you want to execute when the constructor is called. If this is the case its probably either a partial method, an event, or a virtual method that you can override. – Igor Jul 12 '16 at 20:05

2 Answers2

5

You can't. The compiler essentially merges the text of all the partial classes together in to one class when the project is built. You can't have more than one method (including constructors) with the same name and signature per class.

One option is to use a different signature for the constructor, or modify your architecture to not need a constructor. For instance, you could use the existing constructor and have some kind of Initialize method that runs the code from your other constructor.

You could also use "Partial Methods". These are methods marked as partial that you could call from the existing constructor "if they exist". They are designed as extension points for partial classes that come from a code generator, but you may be able to make use of them too. See MSDN for more information.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
4

You can not create multiple constructors with matching signature in to the splitted partial classes because at the compile time both the parts are merged together to generate a single class file. for example

class ClassRoom
{
private int boycount;   //field
public ClassRoom()     //default constructor
{
    boycount = 30;
}
public ClassRoom(int bcount)     //overloaded constructor
{
    boycount = bcount;
}
public double Avg()     //method
{
    //statements goes here
}
}

In above example we can split the class like this.

//Calculation1.cs
partial class ClassRoom
{
private int boycount;   //field

public ClassRoom()     //default constructor
{
    boycount = 30;
}
}
//Calculation2.cs
partial class ClassRoom
{
public ClassRoom(int bcount)     //overloaded constructor
{
    boycount = bcount;
}
public double Avg()     //method
{
    //statements goes here
}
}

Hope it is clear.

Saket Choubey
  • 916
  • 6
  • 11