By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

Accessing the base constructor of a derived class

P: n/a
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
et***********@promega.com

Jun 13 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I think you are looking for:

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

Marc
Jun 13 '07 #2

P: n/a
Thanks! That's it.
Ethan

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I think you are looking for:

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

Marc

Jun 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.