Accessing the base constructor of a derived class

E

Ethan Strauss

Hi,
I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the Master and not in each instance, but this does not seem to be allowed. Is there some way to do it?

The specific example is something like this:

I have created a base class "QCResult" from which "LengthQCResult" (and others) inherit. "QCResult" has a couple of attributes which "LengthQCResult" sets in constructors

public LengthQCResult(QCResultType newResult, LengthQCMethod newMethod)

: base(QCType.Length, newResult)

{

thisMethod = newMethod;

}

public LengthQCResult(QCResultType newResult, string newMethod)

: base(QCType.Length, newResult)

{

switch (newMethod)

{

case "RE":

{

thisMethod = LengthQCMethod.RestrictionDigest;

break;

}

case "PCR":

{

thisMethod = LengthQCMethod.PCR;

break;

}

default:

{

throw new ArgumentException("QC method is not recognized.");

}

}

}

public LengthQCResult()

: base(QCType.Length)

{

}



I realized I need to add a new attribute (IsDatabased) to the base class, but this will be "false" by default and I don't want to have it be a parameter in all constructors for all derived classes. So, I want to make a derived Master constructor (or something like that) which has all the possible parameters and have each of the constructor overloads call the Master. It would look something like:



public LengthQCResult(QCResultType newResult, LengthQCMethod newMethod, bool isInDatabase)

: base(QCType.Length, newResult, isInDatabase)

{

thisMethod = newMethod;

}



And the other constructors would call it:



public LengthQCResult(QCResultType newResult, LengthQCMethod newMethod)

{

this = new LengthQCResult(newResult, newMethod, false);

}

and so forth.

When I try this, I get an error "Cannot assign to '<this>' because it is read-only".

In base classes, I don't have this problem because I can just create a Private MasterConstructor type method which all Public constructors call, but I don't know of a way to access the base constructor in a derived class except from the definition line of a constructor. Is there a way to do that?



Thanks!

Ethan



Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
(e-mail address removed)
 
M

Marc Gravell

I think you are looking for:

public LengthQCResult(QCResultType newResult, LengthQCMethod
newMethod)
: this(newResult, newMethod, false)
{
}

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top